﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.IO;
using TS_XY_Train_Repaire.WPF.UserCon;
using System.Windows.Threading;

namespace TS_XY_Train_Repaire.WPF.Locomotive
{
    /// <summary>
    /// TS_WPF_BaseSet_Map_Edit.xaml 的交互逻辑
    /// </summary>
    public partial class TS_WPF_DigitalMap_Train : UserControl
    {

        public Image _Map = new Image();//系统背景图
        public bool _IsLeftKeyDown = false;//地图鼠标左键是否按下
        public bool _IsTrainLeftKeyDown = false;//火车鼠标左键是否按下
        public bool _IsInfoWindowOpen = false;//详情窗口是否打开
        public Double _MapHeight = 0.00;//背景图原高
        public Double _MapWidth = 0.00;//背景图原宽
        public Double _WorkPlaceHeight = 0.00;//机车工位原高
        public Double _WorkPlaceWidth = 0.00;//机车工位原宽
        public Double _FIWorkPlaceHeight = 0.00;//配件工位原高
        public Double _FIWorkPlaceWidth = 0.00;//配件工位原宽
        public Double _EQWorkPlaceHeight = 0.00;//设备工位原高
        public Double _EQWorkPlaceWidth = 0.00;//设备工位原宽
        public Double _MapX = 0.00;//背景图X坐标
        public Double _MapY = 0.00;//背景图Y坐标
        public Double _ScrollX = 0.00;//背景图缩放比例
        public Double _ScrollY = 0.00;//背景图缩放比例
        public Double _MouseX = 0.00;//鼠标左键按下时X坐标
        public Double _MouseY = 0.00;//鼠标左键按下时Y坐标
        public Double _TrainMouseX = 0.00;//机车鼠标左键按下时X坐标
        public Double _TrainMouseY = 0.00;//机车鼠标左键按下时Y坐标
        public Double _MouseWheelSpeed = 5.00;//鼠标滚轮放大缩小速度
        public Double _ZoomMax = 0.5;//最大放大倍数
        public Double _ZoomMin = 2.00;//最小放大倍数
        public string _sQueryMapResult = "";
        public SortedList<string, RepairTrainSpirit> _img_Train = new SortedList<string, RepairTrainSpirit>();
        public SortedList<string, RepairFittingsSpirit> _img_Fittings = new SortedList<string, RepairFittingsSpirit>();
        public SortedList<string, RepairEqupmentSpirit> _img_Equpment = new SortedList<string, RepairEqupmentSpirit>();
        public SortedList<string, DevExpress.XtraEditors.XtraForm> _img_DevForm = new SortedList<string, DevExpress.XtraEditors.XtraForm>();
        public RepairTrainSpirit _TrainSpiritSelected = new RepairTrainSpirit();
        public RepairFittingsSpirit _FittingsSpiritSelected = new RepairFittingsSpirit();
        public RepairEqupmentSpirit _EqupmentSpiritSelected = new RepairEqupmentSpirit();
        public DispatcherTimer dispatcherTimer = new DispatcherTimer();
        public int iii = 0;
        public TS_WPF_DigitalMap_Train()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            //加载_Map
            Grid_UC.Children.Add(_Map);
            Canvas.SetLeft(_Map, 0);
            Canvas.SetTop(_Map, 0);
            //加载电子地图列表
            _Init_BackGround_List();

            //加载机车控件
            if ((bool)cb_0.IsChecked)
            {
                _InitTrainSpirit();
            }
            if ((bool)cb_1.IsChecked)
            {
                _InitFittingsSpirit();
            }
            if ((bool)cb_2.IsChecked)
            {
                _InitEqupmentSpirit();
            }
            //自动刷新
            _SetSampleImg();
            dispatcherTimer = new DispatcherTimer();
            dispatcherTimer.Tick += new EventHandler(Timer_Tick);
            dispatcherTimer.Interval = TimeSpan.FromMilliseconds(15000);
            dispatcherTimer.Start();
        }
        private void Timer_Tick(object sender, EventArgs e)
        {
            iii++;
            //tb_Text.Text=iii.ToString();
            if (_IsLeftKeyDown || _IsTrainLeftKeyDown || _IsInfoWindowOpen)
            {
            }
            else
            {
                try
                {
                    string strSQL = "select getdate()";
                    SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
                    conn.Open();
                    SqlCommand comm = new SqlCommand(strSQL, conn);
                    SqlDataReader read = comm.ExecuteReader();
                    if ((bool)cb_0.IsChecked)
                    {
                        read.Close();
                        strSQL = "select f_id,f_ttname+'-'+f_name as f_name,f_ttid,f_rpname,f_time,f_tpmapid,(select f_points from TS_S_B_R_Digital_Map_Main_Element where f_del=0 and f_dmmid=" + cb_BackGround_List.SelectedValue.ToString() + " and f_swid=TS_TR_BU_Work_Main.f_tpmapid) as f_points,(select (select f_size from TS_S_B_Digital_Map_Element where f_id=TS_S_B_R_Digital_Map_Main_Element.f_dmeid) from TS_S_B_R_Digital_Map_Main_Element where f_del=0 and f_dmmid=" + cb_BackGround_List.SelectedValue.ToString() + " and f_swid=TS_TR_BU_Work_Main.f_tpmapid) as f_size,(select top 1 f_mwdid from TS_TR_BU_Work_Main_Day where f_del=0 and f_bmwid=TS_TR_BU_Work_Main.f_id and f_state in (select f_id from TS_S_T_S_RP_DataState where f_del=0 and f_state not in (-1,9)) order by f_order asc) as f_mwsid  from TS_TR_BU_Work_Main where f_del=0 and f_state in (select f_id from TS_S_T_S_RP_DataState where f_del=0 and f_state not in (-1,9)) and f_tpmapid in (select f_id from TS_S_B_Station_WorkPlace where f_del=0 " + _sQueryMapResult + ")";
                        comm = new SqlCommand(strSQL, conn);
                        read = comm.ExecuteReader();
                        while (read.Read())
                        {
                            if (Grid_UC.FindName("Train_" + read["f_id"].ToString().Trim()) == null)
                            {
                                LoadTrainSpritControll(read["f_size"].ToString().Trim(), "Train_" + read["f_id"].ToString().Trim(), read["f_name"].ToString().Trim(), read["f_rpname"].ToString().Trim(), read["f_points"].ToString().Trim(), 1, (int)read["f_id"], 1, _GetTrainImg(0, (int)read["f_mwsid"]), (int)read["f_id"], (int)read["f_tpmapid"]);
                            }
                            else
                            {
                                ReLoadTrainSpritControll(read["f_size"].ToString().Trim(), "Train_" + read["f_id"].ToString().Trim(), read["f_name"].ToString().Trim(), read["f_rpname"].ToString().Trim(), read["f_points"].ToString().Trim(), 1, (int)read["f_id"], 1, _GetTrainImg(0, (int)read["f_mwsid"]), (int)read["f_id"], (int)read["f_tpmapid"]);
                            }
                        }
                        _RefreshTrain();
                    }
                    if ((bool)cb_1.IsChecked)
                    {
                        read.Close();
                        strSQL = "select f_id,f_name,(select stuff((select '#' + f_name from TS_S_T_P_TrainDepSys t where f_id in (select f_tdid from TS_FI_BU_Work_Main where f_tpmapid=TS_S_B_Station_WorkPlace.f_id) and f_del = TS_S_T_P_TrainDepSys.f_del for xml path('')) , 1 , 1 , '') as f_name from TS_S_T_P_TrainDepSys where f_id in (select f_tdid from TS_FI_BU_Work_Main where f_tpmapid=TS_S_B_Station_WorkPlace.f_id) group by f_del ) as f_tdlistname,(select f_points from TS_S_B_R_Digital_Map_Main_Element where f_del=0 and f_dmmid=" + cb_BackGround_List.SelectedValue.ToString() + " and f_swid=TS_S_B_Station_WorkPlace.f_id) as f_points,(select (select f_size from TS_S_B_Digital_Map_Element where f_id=TS_S_B_R_Digital_Map_Main_Element.f_dmeid) from TS_S_B_R_Digital_Map_Main_Element where f_del=0 and f_dmmid=" + cb_BackGround_List.SelectedValue.ToString() + " and f_swid=TS_S_B_Station_WorkPlace.f_id) as f_size,(select top 1 f_typeid from TS_FI_BU_Work_Main where f_tpmapid=TS_S_B_Station_WorkPlace.f_id and f_del=0 and f_state in (select f_id from TS_FI_B_Work_Main_State where f_del=0 and f_state<>9) order by f_id asc) as f_ttid,(select top 1 f_mwsid from TS_FI_BU_Work_Main_Steps where f_del=0 and f_bmwid=(select top 1 f_id from TS_FI_BU_Work_Main where f_tpmapid=TS_S_B_Station_WorkPlace.f_id and f_del=0 and f_state in (select f_id from TS_FI_B_Work_Main_State where f_del=0 and f_state<>9) order by f_id asc) and f_state in (select f_id from TS_FI_B_Work_Main_Steps_State where f_del=0 and f_state<>9) order by f_order asc) as f_mwsid from TS_S_B_Station_WorkPlace where f_del=0 and f_id in (select f_tpmapid from TS_FI_BU_Work_Main where f_del=0 and f_state in (select f_id from TS_FI_B_Work_Main_State where f_del=0 and f_state<>9))" + _sQueryMapResult;
                        comm = new SqlCommand(strSQL, conn);
                        read = comm.ExecuteReader();
                        while (read.Read())
                        {
                            if (Grid_UC.FindName("Fittings_" + read["f_id"].ToString().Trim()) == null)
                            {
                                LoadFittingsSpritControll(read["f_size"].ToString().Trim(), "Fittings_" + read["f_id"].ToString().Trim(), read["f_name"].ToString().Trim(), read["f_tdlistname"].ToString().Trim().Replace('#', '\r'), read["f_points"].ToString().Trim(), 1, (int)read["f_id"], 1, _GetFittingsImg((int)read["f_ttid"], (int)read["f_mwsid"]), (int)read["f_id"], (int)read["f_id"]);
                            }
                            else
                            {
                                ReLoadFittingsSpritControll(read["f_size"].ToString().Trim(), "Fittings_" + read["f_id"].ToString().Trim(), read["f_name"].ToString().Trim(), read["f_tdlistname"].ToString().Trim().Replace('#', '\r'), read["f_points"].ToString().Trim(), 1, (int)read["f_id"], 1, _GetFittingsImg((int)read["f_ttid"], (int)read["f_mwsid"]), (int)read["f_id"], (int)read["f_id"]);
                            }
                        }
                        _RefreshFittings();
                    }
                    if ((bool)cb_2.IsChecked)
                    {
                        read.Close();
                        strSQL = "select f_id,f_eqname as f_name,f_etid as f_ttid,f_rpname,f_time,f_tpmapid,(select f_points from TS_S_B_R_Digital_Map_Main_Element where f_del=0 and f_dmmid=" + cb_BackGround_List.SelectedValue.ToString() + " and f_swid=TS_EQ_BU_Work_Main.f_tpmapid) as f_points,(select (select f_size from TS_S_B_Digital_Map_Element where f_id=TS_S_B_R_Digital_Map_Main_Element.f_dmeid) from TS_S_B_R_Digital_Map_Main_Element where f_del=0 and f_dmmid=" + cb_BackGround_List.SelectedValue.ToString() + " and f_swid=TS_EQ_BU_Work_Main.f_tpmapid) as f_size,(select top 1 f_mwsid from TS_EQ_BU_Work_Main_Steps where f_del=0 and f_bmwid=TS_EQ_BU_Work_Main.f_id and f_state in (select f_id from TS_EQ_B_Work_Main_Steps_State where f_del=0 and f_state<>9) order by f_order asc) as f_mwsid  from TS_EQ_BU_Work_Main where f_del=0 and f_state in (select f_id from TS_EQ_B_Work_Main_State where f_del=0 and f_state<>9) and f_tpmapid in (select f_id from TS_S_B_Station_WorkPlace where f_del=0 " + _sQueryMapResult + ")";
                        comm = new SqlCommand(strSQL, conn);
                        read = comm.ExecuteReader();
                        while (read.Read())
                        {
                            if (Grid_UC.FindName("Equpment_" + read["f_id"].ToString().Trim()) == null)
                            {
                                LoadEqupmentSpritControll(read["f_size"].ToString().Trim(), "Equpment_" + read["f_id"].ToString().Trim(), read["f_name"].ToString().Trim(), read["f_rpname"].ToString().Trim(), read["f_points"].ToString().Trim(), 1, (int)read["f_id"], 1, _GetEqupmentImg((int)read["f_ttid"], (int)read["f_mwsid"]), (int)read["f_id"], (int)read["f_tpmapid"]);
                            }
                            else
                            {
                                ReLoadEqupmentSpritControll(read["f_size"].ToString().Trim(), "Equpment_" + read["f_id"].ToString().Trim(), read["f_name"].ToString().Trim(), read["f_rpname"].ToString().Trim(), read["f_points"].ToString().Trim(), 1, (int)read["f_id"], 1, _GetEqupmentImg((int)read["f_ttid"], (int)read["f_mwsid"]), (int)read["f_id"], (int)read["f_tpmapid"]);
                            }
                        }
                        _RefreshEqupment();
                    }
                    read.Close();
                    read.Dispose();
                    comm.Dispose();
                    conn.Close();
                    conn.Dispose();
                }
                catch
                {
                }
            }
            _SetSampleImg();
        }
        private void UserControl_SizeChanged(object sender, System.Windows.SizeChangedEventArgs e)
        {
            _Map.Width = Grid_UC.ActualWidth;
            _Map.Height = Grid_UC.ActualHeight;
            _Map.Stretch = Stretch.Fill;
            _Map.Margin = new System.Windows.Thickness(0, 0, 0, 0);
            Canvas.SetLeft(_Map, 0);
            Canvas.SetTop(_Map, 0);
            if ((bool)cb_0.IsChecked)
            {
                _RefreshTrain();
            }
            if ((bool)cb_1.IsChecked)
            {
                _RefreshFittings();
            }
            if ((bool)cb_2.IsChecked)
            {
                _RefreshEqupment();
            }
            _SetSampleImg();
        }
        private void cb_0_Checked(object sender, System.Windows.RoutedEventArgs e)
        {
            if ((bool)cb_0.IsChecked)
            {
                _InitTrainSpirit();
            }
            else
            {
                try
                {
                    _CleanTrainSpirit();
                }
                catch
                {
                }
            }
            if ((bool)cb_1.IsChecked)
            {
                _InitFittingsSpirit();
            }
            else
            {
                try
                {
                    _CleanFittingsSpirit();
                }
                catch
                {
                }
            }
            if ((bool)cb_2.IsChecked)
            {
                _InitEqupmentSpirit();
            }
            else
            {
                try
                {
                    _CleanEqupmentSpirit();
                }
                catch
                {
                }
            }
            _SetSampleImg();
        }
        #region//--地图事件--//
        private void _Init_BackGround_List()
        {
            DataTable _dt = new DataTable();
            TS_Win_Class.FilledDataTable("select f_id,f_name from TS_S_B_Digital_Map_Main where f_del=0", _dt);
            DataView _dv = _dt.DefaultView;
            cb_BackGround_List.ItemsSource = _dv;
            cb_BackGround_List.DisplayMemberPath = "f_name";
            cb_BackGround_List.SelectedValuePath = "f_id";
            if (cb_BackGround_List.Items.Count > 0)
            {
                cb_BackGround_List.SelectedIndex = 0;
            }
        }

        private void cb_BackGround_List_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            _sQueryMapResult = " and (f_level like '%,-1,%'";
            if (cb_BackGround_List.SelectedIndex > -1)
            {
                string strSQL = "select f_swid as f_id from TS_S_B_R_Digital_Map_Main_Station where f_del=0 and f_dmid=" + cb_BackGround_List.SelectedValue.ToString();
                SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
                conn.Open();
                SqlCommand comm = new SqlCommand(strSQL, conn);
                SqlDataReader read = comm.ExecuteReader();
                while (read.Read())
                {
                    _sQueryMapResult += " or f_level like '%,"+read["f_id"].ToString()+",%'";
                }
                read.Close();
                read.Dispose();
                comm.Dispose();
                conn.Close();
                conn.Dispose();
            }
            _sQueryMapResult += ")";
            if (cb_BackGround_List.SelectedIndex > -1)
            {
                _LoadInsertImageByID(Convert.ToInt16(cb_BackGround_List.SelectedValue));
                //_RefreshTrain();
            }
            if ((bool)cb_0.IsChecked)
            {
                _InitTrainSpirit();
            }
            if ((bool)cb_1.IsChecked)
            {
                _InitFittingsSpirit();
            }
            if ((bool)cb_2.IsChecked)
            {
                _InitEqupmentSpirit();
            }
        }
        /// <summary>
        /// 载入指定的数字地图
        /// </summary>
        /// <param name="_ID"></param>
        private void _LoadInsertImageByID(int _ID)
        {
            string _str = TS_Win_Class.GetSQlFieldValue("select f_brief from TS_S_B_Digital_Map_Main where f_id=" + _ID.ToString(), "f_brief");
            try
            {
                if (_str.Length > 100)
                {
                    byte[] bt = Convert.FromBase64String(_str);
                    System.IO.MemoryStream stream = new System.IO.MemoryStream(bt);
                    stream.Seek(0, SeekOrigin.Begin);
                    BitmapImage bitmapImage;
                    bitmapImage = new BitmapImage();
                    bitmapImage.BeginInit();

                    bitmapImage.StreamSource = stream;

                    bitmapImage.EndInit();
                    _MapHeight = bitmapImage.Height;
                    _MapWidth = bitmapImage.Width;
                    _Map.Width = Grid_UC.ActualWidth;
                    _Map.Height = Grid_UC.ActualHeight;
                    _Map.Name = "BackGround";
                    _Map.Source = bitmapImage;
                    _Map.Stretch = Stretch.Fill;
                    Canvas.SetLeft(_Map, 0);
                    Canvas.SetTop(_Map, 0);
                    if ((_Map.Height / _MapHeight) < _ZoomMin)
                    {
                        _ZoomMin = (_Map.Height / _MapHeight) - 0.1;
                    }
                    if ((_Map.Width / _MapWidth) < _ZoomMin)
                    {
                        _ZoomMin = (_Map.Width / _MapWidth) - 0.1;
                    }
                    if ((_Map.Height / _MapHeight) > _ZoomMax)
                    {
                        _ZoomMax = (_Map.Height / _MapHeight) + 0.1;
                    }
                    if ((_Map.Width / _MapWidth) > _ZoomMax)
                    {
                        _ZoomMax = (_Map.Width / _MapWidth) + 0.1;
                    }
                }
            }
            catch
            {
            }
        }
        #endregion
        #region//--窗体鼠标事件--//
        private void Grid_UC_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (_IsLeftKeyDown)
            {
                //_Map.Margin = new System.Windows.Thickness(_MapX - _MouseX + e.GetPosition(Grid_UC).X, _MapY - _MouseY + e.GetPosition(Grid_UC).Y , 0, 0);
                Canvas.SetLeft(_Map, Canvas.GetLeft(_Map) - (_MouseX - e.GetPosition(Grid_UC).X));
                Canvas.SetTop(_Map, Canvas.GetTop(_Map) - (_MouseY - e.GetPosition(Grid_UC).Y));
                if ((bool)cb_0.IsChecked)
                {
                    _RefreshTrain();
                }
                if ((bool)cb_1.IsChecked)
                {
                    _RefreshFittings();
                }
                if ((bool)cb_2.IsChecked)
                {
                    _RefreshEqupment();
                }
                _MouseX = e.GetPosition(Grid_UC).X;
                _MouseY = e.GetPosition(Grid_UC).Y;
            }
            _SetSampleImg();
        }

        private void Grid_UC_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (_IsInfoWindowOpen || _IsLeftKeyDown)
            {
                _IsLeftKeyDown = false;
            }
            else
            {
                if (!_IsTrainLeftKeyDown && sender.GetType().ToString() != "RepairTrainSpirit")
                {
                    //tb_Text.Text = sender.GetType().ToString().Substring(sender.GetType().ToString().LastIndexOf('.') + 1);
                    _IsLeftKeyDown = true;
                    _MouseX = e.GetPosition(Grid_UC).X;
                    _MouseY = e.GetPosition(Grid_UC).Y;
                }
                else
                {
                    _IsLeftKeyDown = false;
                }
            }
        }

        private void Grid_UC_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (_IsLeftKeyDown)
            {
                _IsLeftKeyDown = false;
            }
        }

        private void Grid_UC_MouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e)
        {
            if (_IsInfoWindowOpen)
            {
            }
            else
            {
                //if (((_Map.Height / _MapHeight) >= _ZoomMin && (_Map.Height / _MapHeight) <= _ZoomMax) || ((_Map.Height / _MapHeight) < _ZoomMin && (double)e.Delta > 0) || ((_Map.Height / _MapHeight) > _ZoomMax && (double)e.Delta < 0))
                //{
                    try
                    {
                        _Map.Height += (double)e.Delta * _MouseWheelSpeed / 5;
                        _Map.Width += (double)e.Delta * _MouseWheelSpeed / 5;
                        Canvas.SetLeft(_Map, Canvas.GetLeft(_Map) - (double)e.Delta * _MouseWheelSpeed / 5 / 2);
                        Canvas.SetTop(_Map, Canvas.GetTop(_Map) - (double)e.Delta * _MouseWheelSpeed / 5 / 2);
                        if ((bool)cb_0.IsChecked)
                        {
                            _RefreshTrain();
                        }
                        if ((bool)cb_1.IsChecked)
                        {
                            _RefreshFittings();
                        }
                        if ((bool)cb_2.IsChecked)
                        {
                            _RefreshEqupment();
                        }
                    }
                    catch
                    {
                    }
                //}
            }
        }
        #endregion
        #region//--火车控件事件--//
        // 初始化火车控件
        private void _InitTrainSpirit()
        {
            _CleanTrainSpirit();
            string strSQL = "select f_id,f_ttname+'-'+f_name as f_name,f_ttid,f_rpname,f_time,f_tpmapid,(select f_points from TS_S_B_R_Digital_Map_Main_Element where f_del=0 and f_dmmid=" + cb_BackGround_List.SelectedValue.ToString() + " and f_swid=TS_TR_BU_Work_Main.f_tpmapid) as f_points,(select (select f_size from TS_S_B_Digital_Map_Element where f_id=TS_S_B_R_Digital_Map_Main_Element.f_dmeid) from TS_S_B_R_Digital_Map_Main_Element where f_del=0 and f_dmmid=" + cb_BackGround_List.SelectedValue.ToString() + " and f_swid=TS_TR_BU_Work_Main.f_tpmapid) as f_size,isnull((select top 1 f_mwdid from TS_TR_BU_Work_Main_Day where f_del=0 and f_bmwid=TS_TR_BU_Work_Main.f_id and f_state in (select f_id from TS_S_T_S_RP_DataState where f_del=0 and f_state not in (-1,9)) order by f_order asc),0) as f_mwsid  from TS_TR_BU_Work_Main where f_del=0 and f_state in (select f_id from TS_S_T_S_RP_DataState where f_del=0 and f_state not in (-1,9)) and f_tpmapid in (select f_id from TS_S_B_Station_WorkPlace where f_del=0 " + _sQueryMapResult + ")";
//            MessageBox.Show(strSQL);
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            SqlCommand comm = new SqlCommand(strSQL, conn);
            SqlDataReader read = comm.ExecuteReader();
            while (read.Read())
            {
                if (Grid_UC.FindName("Train_" + read["f_id"].ToString().Trim()) == null)
                {
                    LoadTrainSpritControll(read["f_size"].ToString().Trim(), "Train_" + read["f_id"].ToString().Trim(), read["f_name"].ToString().Trim(), read["f_rpname"].ToString().Trim(), read["f_points"].ToString().Trim(), 1, (int)read["f_id"], 1, _GetTrainImg(0, (int)read["f_mwsid"]), (int)read["f_id"], (int)read["f_tpmapid"]);
                }
            }
            read.Close();
            read.Dispose();
            comm.Dispose();
            conn.Close();
            conn.Dispose();
        }
        private void LoadTrainSpritControll(string sSize, string sName, string sTrainName, string sRPName, string sXY, int iTrainState, int iTrainID, int iWorkState, BitmapImage _Image, int iBMWID, int iWorkPlaceID)
        {
            double _x, _y;
            _WorkPlaceWidth = Convert.ToDouble(sSize.Substring(0, sSize.IndexOf(',')));
            _WorkPlaceHeight = Convert.ToDouble(sSize.Substring(sSize.IndexOf(',') + 1));
            RepairTrainSpirit Spirit = new RepairTrainSpirit();
            Spirit.ToolTip = TrainSpiritToolTip(iBMWID.ToString());
            Spirit.Name = sName;
            Spirit.iBMWID = iBMWID;
            Spirit.iWorkPlaceID = iWorkPlaceID;
            Spirit.iTrainID = iTrainID;
            Spirit.iTrainState = iTrainState;
            Spirit.iWorkState = iWorkState;
            Spirit.MouseMove +=new System.Windows.Input.MouseEventHandler(TrainMouseMove);
            Spirit.MouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(TrainMouseLeftButtonDown);
            Spirit.MouseLeftButtonUp += new System.Windows.Input.MouseButtonEventHandler(TrainMouseLeftButtonUp);
//            Spirit.MouseRightButtonDown += new System.Windows.Input.MouseButtonEventHandler(TrainMouseRightButtonDown);
            Spirit.MouseRightButtonDown += new System.Windows.Input.MouseButtonEventHandler(TrainMouseDoubleClick);
            Spirit.MouseDoubleClick += new System.Windows.Input.MouseButtonEventHandler(TrainMouseDoubleClick_1);
            Spirit._Name.Text = sTrainName;
            Spirit._Name.Fill = new SolidColorBrush(Colors.YellowGreen);
            Spirit._RPName.Text = sRPName;
            Spirit._RPName.Fill = new SolidColorBrush(Colors.YellowGreen);
            Spirit._image.Source = _Image;
            _x = Convert.ToDouble(sXY.Substring(0, sXY.IndexOf(',')));
            _y = Convert.ToDouble(sXY.Substring(sXY.IndexOf(',') + 1));
            Spirit.CenterX = _x;
            Spirit.CenterY = _y;
            Spirit.Width = _WorkPlaceWidth / (_MapWidth / _Map.Width);
            Spirit.Height = _WorkPlaceHeight * 3.00  / (_MapHeight / _Map.Height);
            Spirit._grid.Width = _WorkPlaceWidth / (_MapWidth / _Map.Width);
            Spirit._grid.Height = _WorkPlaceHeight * 3.00 / (_MapHeight / _Map.Height);
            Spirit.iHeight = _Image.Height;
            Spirit.iWidth = _Image.Width;
            Spirit._image.Width = _WorkPlaceWidth  / (_MapWidth / _Map.Width);
            Spirit._image.Height = _WorkPlaceHeight  / (_MapHeight / _Map.Height);
            Spirit._Name.Margin = new System.Windows.Thickness(0, 0 - Spirit.Height * 2.00 / 3.00, 0, 0);
            Spirit._RPName.Margin = new System.Windows.Thickness(0, Spirit.Height * 2.00 / 3.00 + Spirit._image.Height / 2.00, 0, 0);
            Spirit._image.Margin = new System.Windows.Thickness(0, Spirit.Height / 3.00, 0, 0);
            //Spirit._Name.Margin = new System.Windows.Thickness(0, 0 - Spirit.Height * 2.00 / 3.00, 0, 0);
            //Spirit._RPName.Margin = new System.Windows.Thickness(0, Spirit.Height * 2.00 / 3.00+Spirit._image.Height/2.00, 0, 0);
            //Spirit._image.Margin = new System.Windows.Thickness(0, Spirit.Height*2 / 3.00, 0, 0);
            if (Spirit._image.Height <= 12.00)
            {
                Spirit._RPName.FontSize = 6.00;
                Spirit._Name.FontSize = 6.00;
            }
            if (Spirit._image.Height <= 24.00 && Spirit._image.Height > 12.00)
            {
                Spirit._RPName.FontSize = 9.00;
                Spirit._Name.FontSize = 9.00;
            }
            if (Spirit._image.Height <= 36.00 && Spirit._image.Height > 24.00)
            {
                Spirit._RPName.FontSize = 12.00;
                Spirit._Name.FontSize = 12.00;
            }
            if (Spirit._image.Height > 36.00)
            {
                Spirit._RPName.FontSize = 16.00;
                Spirit._Name.FontSize = 16.00;
            }
            _img_Train.Add(Spirit.Name, Spirit);
            Grid_UC.RegisterName(Spirit.Name, Spirit);
            Grid_UC.Children.Add(Spirit);
            //Spirit.Background = new SolidColorBrush(Colors.White);
            Panel.SetZIndex(Spirit, 2);
            Canvas.SetLeft(Spirit, (_x) / (_MapWidth / _Map.Width) + _MapX);
            //Canvas.SetTop(Spirit, (_y - _WorkPlaceHeight) / (_MapHeight / _Map.Height) + _MapY);// -  
            Canvas.SetTop(Spirit, (_y + _WorkPlaceHeight) / (_MapHeight / _Map.Height) - Spirit._image.Height * 7.00 / 4.00 + _MapY);// -  
        }
        private void ReLoadTrainSpritControll(string sSize, string sName, string sTrainName, string sRPName, string sXY, int iTrainState, int iTrainID, int iWorkState, BitmapImage _Image, int iBMWID, int iWorkPlaceID)
        {
            double _x, _y;
            _WorkPlaceWidth = Convert.ToDouble(sSize.Substring(0, sSize.IndexOf(',')));
            _WorkPlaceHeight = Convert.ToDouble(sSize.Substring(sSize.IndexOf(',') + 1));
            RepairTrainSpirit Spirit = (RepairTrainSpirit)Grid_UC.FindName(sName);
            Spirit.ToolTip = TrainSpiritToolTip(iBMWID.ToString());
            Spirit.Name = sName;
            Spirit.iBMWID = iBMWID;
            Spirit.iWorkPlaceID = iWorkPlaceID;
            Spirit.iTrainID = iTrainID;
            Spirit.iTrainState = iTrainState;
            Spirit.iWorkState = iWorkState;
            Spirit._Name.Text = sTrainName;
            Spirit._Name.Fill = new SolidColorBrush(Colors.YellowGreen);
            Spirit._RPName.Text = sRPName;
            Spirit._RPName.Fill = new SolidColorBrush(Colors.YellowGreen);
            Spirit._image.Source = _Image;
            _x = Convert.ToDouble(sXY.Substring(0, sXY.IndexOf(',')));
            _y = Convert.ToDouble(sXY.Substring(sXY.IndexOf(',') + 1));
            Spirit.CenterX = _x;
            Spirit.CenterY = _y;
            Spirit.Width = _WorkPlaceWidth / (_MapWidth / _Map.Width);
            Spirit.Height = _WorkPlaceHeight * 3.00 / (_MapHeight / _Map.Height);
            Spirit._grid.Width = _WorkPlaceWidth / (_MapWidth / _Map.Width);
            Spirit._grid.Height = _WorkPlaceHeight * 3.00 / (_MapHeight / _Map.Height);
            Spirit.iHeight = _Image.Height;
            Spirit.iWidth = _Image.Width;
            Spirit._image.Width = _WorkPlaceWidth / (_MapWidth / _Map.Width);
            Spirit._image.Height = _WorkPlaceHeight / (_MapHeight / _Map.Height);
            Spirit._Name.Margin = new System.Windows.Thickness(0, 0 - Spirit.Height * 2.00 / 3.00, 0, 0);
            Spirit._RPName.Margin = new System.Windows.Thickness(0, Spirit.Height * 2.00 / 3.00 + Spirit._image.Height / 2.00, 0, 0);
            Spirit._image.Margin = new System.Windows.Thickness(0, Spirit.Height / 3.00, 0, 0);
            if (Spirit._image.Height <= 12.00)
            {
                Spirit._RPName.FontSize = 6.00;
                Spirit._Name.FontSize = 6.00;
            }
            if (Spirit._image.Height <= 24.00 && Spirit._image.Height > 12.00)
            {
                Spirit._RPName.FontSize = 9.00;
                Spirit._Name.FontSize = 9.00;
            }
            if (Spirit._image.Height <= 36.00 && Spirit._image.Height > 24.00)
            {
                Spirit._RPName.FontSize = 12.00;
                Spirit._Name.FontSize = 12.00;
            }
            if (Spirit._image.Height > 36.00)
            {
                Spirit._RPName.FontSize = 16.00;
                Spirit._Name.FontSize = 16.00;
            }
        }
        private ToolTip TrainSpiritToolTip(string _BMWID)
        {
            ToolTip ttp = new ToolTip();
            StackPanel _sp = new StackPanel();
            string strSQL = "select (select f_string from TS_S_T_S_ColorList where f_id=(select f_cid from TS_S_T_S_RP_DataState where f_id=TS_TR_BU_Work_Main.f_state)) as f_clcode,f_id,CONVERT(varchar(10), f_stime, 120) as f_stime,CONVERT(varchar(10), f_etime, 120) as f_etime,f_ttname+'-'+f_name as f_name,f_ttid,f_rpname as f_rpnum,(select f_name from TS_S_T_P_Train_RP where f_id=TS_TR_BU_Work_Main.f_rpid) as f_rpname,f_time,f_tpmapid,(select f_name from TS_S_B_Station_WorkPlace where f_id=TS_TR_BU_Work_Main.f_tpmapid) as f_mapname,(select top 1 f_name from TS_TR_BU_Work_Main_Day where f_del=0 and f_bmwid=TS_TR_BU_Work_Main.f_id and f_state in (select f_id from TS_S_T_S_RP_DataState where f_del=0 and f_state not in (-1,9)) order by f_order asc) as f_mwsname  from TS_TR_BU_Work_Main where f_del=0 and f_id=" + _BMWID;
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            SqlCommand comm = new SqlCommand(strSQL, conn);
            SqlDataReader read = comm.ExecuteReader();
            while (read.Read())
            {
                TextBlock _tb = new TextBlock();
                switch (read["f_clcode"].ToString())
                {
                    case "Gray":
                        _tb.Background = new SolidColorBrush(Colors.Gray);
                        break;
                    case "Red":
                        _tb.Background = new SolidColorBrush(Colors.Red);
                        break;
                    case "Yellow":
                        _tb.Background = new SolidColorBrush(Colors.Yellow);
                        break;
                    case "LightBlue":
                        _tb.Background = new SolidColorBrush(Colors.LightBlue);
                        break;
                    case "Magenta":
                        _tb.Background = new SolidColorBrush(Colors.Magenta);
                        break;
                    case "Green":
                        _tb.Background = new SolidColorBrush(Colors.Green);
                        break;
                    case "White":
                        _tb.Background = new SolidColorBrush(Colors.White);
                        break;
                    default:
                        _tb.Background = new SolidColorBrush(Colors.White);
                        break;
                }
                _tb.Text = "机车号:" + read["f_name"].ToString() + "\r";
                _tb.Text += "修程:" + read["f_rpname"].ToString() + "\r";
                _tb.Text += "修次:" + read["f_rpnum"].ToString() + "\r";
                _tb.Text += "作业位置:" + read["f_mapname"].ToString() + "\r";
                _tb.Text += "作业状态:" + read["f_mwsname"].ToString() + "\r";
                _tb.Text += "入库时间:" + read["f_stime"].ToString() + "\r";
                _tb.Text += "计划出库时间:" + read["f_etime"].ToString() + "\r";
                _sp.Children.Add(_tb);
            }
            read.Close();
            read.Dispose();
            comm.Dispose();
            conn.Close();
            conn.Dispose();

            ttp.Content = _sp;
            //ToolTipService.SetShowDuration(ttp, 60000);
            return (ttp);
        }
        private void _RefreshTrain()
        {
            RepairTrainSpirit Spirit = new RepairTrainSpirit();
            foreach (KeyValuePair<string, RepairTrainSpirit> _Train_sort in _img_Train)
            {
                Spirit = _Train_sort.Value;
                if (Spirit != null)
                {
                    Spirit.Width = _WorkPlaceWidth / (_MapWidth / _Map.Width);
                    Spirit.Height = _WorkPlaceHeight * 3 / (_MapHeight / _Map.Height);
                    Spirit.iHeight = _WorkPlaceHeight * 3 / (_MapHeight / _Map.Height);
                    Spirit.iWidth = _WorkPlaceWidth / (_MapWidth / _Map.Width);
                    Spirit._image.Width = _WorkPlaceWidth / (_MapWidth / _Map.Width);
                    Spirit._image.Height = _WorkPlaceHeight / (_MapHeight / _Map.Height);
                    Spirit._grid.Width = _WorkPlaceWidth / (_MapWidth / _Map.Width);
                    Spirit._grid.Height = _WorkPlaceHeight * 3 / (_MapHeight / _Map.Height);
                    Spirit._Name.Margin = new System.Windows.Thickness(0, 0 - Spirit.Height * 2 / 3, 0, 0);
                    Spirit._RPName.Margin = new System.Windows.Thickness(0, Spirit.Height * 2 / 3 + Spirit._image.Height / 2, 0, 0);
                    Spirit._image.Margin = new System.Windows.Thickness(0, Spirit.Height / 3, 0, 0);
                    if (Spirit._image.Height <= 12.00)
                    {
                        Spirit._RPName.FontSize = 6.00;
                        Spirit._Name.FontSize = 6.00;
                    }
                    if (Spirit._image.Height <= 24.00 && Spirit._image.Height > 12.00)
                    {
                        Spirit._RPName.FontSize = 9.00;
                        Spirit._Name.FontSize = 9.00;
                    }
                    if (Spirit._image.Height <= 36.00 && Spirit._image.Height > 24.00)
                    {
                        Spirit._RPName.FontSize = 12.00;
                        Spirit._Name.FontSize = 12.00;
                    }
                    if (Spirit._image.Height > 36.00)
                    {
                        Spirit._RPName.FontSize = 16.00;
                        Spirit._Name.FontSize = 16.00;
                    }
                    Canvas.SetLeft(Spirit, (Spirit.CenterX) / (_MapWidth / _Map.Width) + Canvas.GetLeft(_Map));
                    Canvas.SetTop(Spirit, (Spirit.CenterY + _WorkPlaceHeight) / (_MapHeight / _Map.Height) - Spirit._image.Height * 7.00/4.00 + Canvas.GetTop(_Map));// - Spirit.Height*2/3
                }
                else
                {

                }
            }

        }
        private void _CleanTrainSpirit()
        {
            foreach (KeyValuePair<string, RepairTrainSpirit> _Train_sort in _img_Train)
            {
                Grid_UC.UnregisterName(_Train_sort.Key);
                Grid_UC.Children.Remove(_Train_sort.Value);
            }
            _img_Train.Clear();
        }
        private BitmapImage _GetTrainImg(int _ttid,int _mwsid)
        {
            BitmapImage bitmapImage;
            bitmapImage = new BitmapImage();
            string _str = "";
            try
            {
                _str = TS_Win_Class.GetSQlFieldValue("select isnull((select top 1 f_brief from TS_S_B_Digital_Map_WorkState where f_del=0 and f_funcid=" + _mwsid.ToString()+"),'-') as f_brief", "f_brief");
            }
            catch
            {
            }
            try
            {
                if (_str.Length > 100)
                {
                }
                else
                {
                    _str = TS_Win_Class.GetSQlFieldValue("select top 1 f_brief from TS_S_B_Digital_Map_WorkState where f_id=-1", "f_brief");
                }
                byte[] bt = Convert.FromBase64String(_str);
                System.IO.MemoryStream stream = new System.IO.MemoryStream(bt);
                stream.Seek(0, SeekOrigin.Begin);
                bitmapImage.BeginInit();

                bitmapImage.StreamSource = stream;

                bitmapImage.EndInit();
            }
            catch
            {
            }
            return bitmapImage;
        }
        private void TrainMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (_IsInfoWindowOpen)
            {
            }
            else
            {
                if (_TrainSpiritSelected != null && _IsTrainLeftKeyDown)
                {
                    Canvas.SetLeft(_TrainSpiritSelected, e.GetPosition(Grid_UC).X - _TrainMouseX);
                    Canvas.SetTop(_TrainSpiritSelected, (e.GetPosition(Grid_UC).Y) - _TrainMouseY);
                }
            }
        }
        private void TrainMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            //tb_Text.Text = _IsInfoWindowOpen.ToString() + _IsTrainLeftKeyDown.ToString();
            if (_IsLeftKeyDown || _IsInfoWindowOpen)
            {
                _IsTrainLeftKeyDown = false;
                _TrainSpiritSelected = null;
            }
            else
            {
                dispatcherTimer.Stop();
                _IsTrainLeftKeyDown = true;
                _TrainSpiritSelected = (RepairTrainSpirit)sender;
                _TrainMouseX = e.GetPosition(Grid_UC).X - Canvas.GetLeft(_TrainSpiritSelected);
                _TrainMouseY = e.GetPosition(Grid_UC).Y - Canvas.GetTop(_TrainSpiritSelected);
                //tb_Text.Text = sender.GetType().ToString().Substring(sender.GetType().ToString().LastIndexOf('.') + 1);
            }
        }
        private void TrainMouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (_IsTrainLeftKeyDown)
            {
                Double _x = 0.00;
                Double _y = 0.00;
                string strSQL = "select * from TS_S_B_R_Digital_Map_Main_Element where f_del=0 and f_dmmid=" + cb_BackGround_List.SelectedValue.ToString() + " and f_swid in (select f_id from TS_S_B_Station_WorkPlace where f_del=0 and f_stack>f_stacknum and f_type in (0))";
                SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
                conn.Open();
                SqlCommand comm = new SqlCommand(strSQL, conn);
                SqlDataReader read = comm.ExecuteReader();
                while (read.Read())
                {
                    _x = Convert.ToDouble(read["f_points"].ToString().Substring(0, read["f_points"].ToString().IndexOf(',')));
                    _y = Convert.ToDouble(read["f_points"].ToString().Substring(read["f_points"].ToString().IndexOf(',') + 1));
                    if (e.GetPosition(_Map).X >= _x * (_Map.Width / _MapWidth) && e.GetPosition(_Map).X <= _x * (_Map.Width / _MapWidth) + _TrainSpiritSelected.Width && e.GetPosition(_Map).Y <= _y * (_Map.Height / _MapHeight) + _TrainSpiritSelected.Height && e.GetPosition(_Map).Y >= _y * (_Map.Height / _MapHeight))
                    {
                        //tb_Text.Text = "找到工位";
                        strSQL = "update TS_S_B_Station_WorkPlace set f_stacknum=f_stacknum-1 where f_id=" + _TrainSpiritSelected.iWorkPlaceID.ToString();
                        TS_Win_Class.MySqlCom(strSQL);
                        strSQL = "update TS_S_B_Station_WorkPlace set f_stacknum=f_stacknum+1 where f_id=" + read["f_swid"].ToString();
                        TS_Win_Class.MySqlCom(strSQL);
                        strSQL = "update TS_TR_BU_Work_Main set f_tpmapid=" + read["f_swid"].ToString() + " where f_id=" + _TrainSpiritSelected.iBMWID.ToString();
                        TS_Win_Class.MySqlCom(strSQL);
                        _TrainSpiritSelected.CenterX = _x;
                        _TrainSpiritSelected.CenterY = _y;
                        _TrainSpiritSelected.iWorkPlaceID = (int)read["f_swid"];
                        break;
                    }
                }
                read.Close();
                read.Dispose();
                comm.Dispose();
                conn.Close();
                conn.Dispose();
                _RefreshTrain();
            }
            dispatcherTimer.Start();
            _IsTrainLeftKeyDown = false;
            _TrainSpiritSelected = null;
            //tb_Text.Text = "左键弹起";
        }
        private void TrainMouseRightButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            //_IsInfoWindowOpen = true;
            //_IsTrainLeftKeyDown = false;
            //_TrainSpiritSelected = (RepairTrainSpirit)sender;
            //Pub.MFD.Locomotive.Train.Pub_MDF_Locomotive_Train_Main_Work_Show_Form _uc = new Pub.MFD.Locomotive.Train.Pub_MDF_Locomotive_Train_Main_Work_Show_Form();
            //_uc._BMWID=_TrainSpiritSelected.iBMWID.ToString();
            //if (_uc.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            //{
            //    _IsInfoWindowOpen = false;
            //    _IsTrainLeftKeyDown = false;
            //    _TrainSpiritSelected = null;
            //}
            //else
            //{
            //    _IsInfoWindowOpen = false ;
            //    _IsTrainLeftKeyDown = false;
            //    _TrainSpiritSelected = null;
            //}
            //_TrainMouseX = 0.00;
            //_TrainMouseY = 0.00;
        }
        private void TrainMouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            //try
            //{
            //    for (int i = 0; i < _img_DevForm.Count - 5; i++)
            //    {
            //        ((DevExpress.XtraEditors.XtraForm)_img_DevForm["_video" + i.ToString()]).Close();
            //    }
            //    ((DevExpress.XtraEditors.XtraForm)_img_DevForm["MFD"]).Close();
            //    ((DevExpress.XtraEditors.XtraForm)_img_DevForm["DEP"]).Close();
            //    ((DevExpress.XtraEditors.XtraForm)_img_DevForm["REP"]).Close();
            //    ((DevExpress.XtraEditors.XtraForm)_img_DevForm["IMG"]).Close();
            //    ((DevExpress.XtraEditors.XtraForm)_img_DevForm["VOI"]).Close();
            //    _img_DevForm.Clear();
            //}
            //catch
            //{
            //}
            //_IsInfoWindowOpen = true;
            //_IsTrainLeftKeyDown = false;
            //_TrainSpiritSelected = (RepairTrainSpirit)sender;
            //TS_Win_DJD.UserCon.Video.Video_Main_Form _video = new TS_Win_DJD.UserCon.Video.Video_Main_Form();
            //string strSQL = "select * from TS_S_B_Station_Camera where f_del=0 and f_id in (select f_cid from TS_S_B_Station_WorkPlace_Camera where f_del=0 and f_swid=" + _TrainSpiritSelected.iWorkPlaceID.ToString()+")";
            //SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            //conn.Open();
            //SqlCommand comm = new SqlCommand(strSQL, conn);
            //SqlDataReader read = comm.ExecuteReader();
            //if (read.HasRows)
            //{
            //    int i = 0;
            //    while (read.Read())
            //    {
            //        _video = new TS_Win_DJD.UserCon.Video.Video_Main_Form();
            //        _video.Text = read["f_name"].ToString();
            //        _video._ip = read["f_ip"].ToString();
            //        _video._port = read["f_port"].ToString();
            //        _video._us = read["f_logname"].ToString();
            //        _video._ps = read["f_logpass"].ToString();
            //        _img_DevForm.Add("_video"+i.ToString(), _video);
            //        _video.Show();
            //        _video.Size=new System.Drawing.Size(200,200);
            //        switch (i)
            //        {
            //            case 0:
            //                _video.Location = new System.Drawing.Point(10, 10);
            //                break;
            //            case 1:
            //                _video.Location = new System.Drawing.Point(200, 10);
            //                break;
            //            case 2:
            //                _video.Location = new System.Drawing.Point(400, 10);
            //                break;
            //            case 3:
            //                _video.Location = new System.Drawing.Point(10, 200);
            //                break;
            //            case 4:
            //                _video.Location = new System.Drawing.Point(200, 200);
            //                break;
            //            case 5:
            //                _video.Location = new System.Drawing.Point(400, 200);
            //                break;
            //        }
            //            i++;
            //    }
            //}
            //else
            //{
            //    MessageBox.Show("该工位附近没有绑定摄像头！");
            //}
            //read.Close();
            //read.Dispose();
            //comm.Dispose();
            //conn.Close();
            //conn.Dispose();
            //Pub.Repaire.Loco.Pub_Repaire_Loco_DepList _dl = new Pub.Repaire.Loco.Pub_Repaire_Loco_DepList();
            //_dl._BMWID = _TrainSpiritSelected.iBMWID.ToString();
            //_dl.Height = 200;
            //_dl.Show();
            //_dl.Location = new System.Drawing.Point(600 + (Convert.ToInt16(Grid_UC.ActualWidth) - 800) * 0 / 3, 10);
            //_img_DevForm.Add("DEP", _dl);
            //Pub.Repaire.Loco.Pub_Repaire_Loco_RepList _rl = new Pub.Repaire.Loco.Pub_Repaire_Loco_RepList();
            //_rl._BMWID = _TrainSpiritSelected.iBMWID.ToString();
            //_rl.Height = 200;
            //_rl.Show();
            //_rl.Location = new System.Drawing.Point(600 + (Convert.ToInt16(Grid_UC.ActualWidth) - 800) * 1 / 3, 10);
            //_img_DevForm.Add("REP", _rl);
            //Pub.Repaire.Loco.Pub_Repaire_Loco_WorkList _wl = new Pub.Repaire.Loco.Pub_Repaire_Loco_WorkList();
            //_wl._BMWID = _TrainSpiritSelected.iBMWID.ToString();
            //_wl.Height = 200;
            //_wl.Show();
            //_wl.Location = new System.Drawing.Point(600 + (Convert.ToInt16(Grid_UC.ActualWidth) - 800) * 2 / 3, 10);
            //_img_DevForm.Add("IMG", _wl);
            //Pub.Repaire.Loco.Pub_Repaire_Loco_VoiceList _vl = new Pub.Repaire.Loco.Pub_Repaire_Loco_VoiceList();
            //_vl._BMWID = _TrainSpiritSelected.iBMWID.ToString();
            //_vl.Height = 200;
            //_vl.Show();
            //_vl.Location = new System.Drawing.Point(600 + (Convert.ToInt16(Grid_UC.ActualWidth) - 800) * 3 / 3, 10);
            //_img_DevForm.Add("VOI", _vl);
            //Pub.MFD.Locomotive.Train.Pub_MDF_Locomotive_Train_Main_Work_Show_Form _uc = new Pub.MFD.Locomotive.Train.Pub_MDF_Locomotive_Train_Main_Work_Show_Form();
            //_uc._BMWID = _TrainSpiritSelected.iBMWID.ToString();
            //_uc.Show();
            //_uc.Location = new System.Drawing.Point(10, 620);
            //_img_DevForm.Add("MFD", _uc);
            //_IsInfoWindowOpen = false;
            //_IsTrainLeftKeyDown = false;
            //_TrainSpiritSelected = null;
            //_TrainMouseX = 0.00;
            //_TrainMouseY = 0.00;
        }
        private void TrainMouseDoubleClick_1(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            //_IsTrainLeftKeyDown = false;
            //_TrainSpiritSelected = (RepairTrainSpirit)sender;
            //TS_Win_DJD.Locomotive.Repair.TrainRepair.Locomotive_Repair_OneTrain_Work_Main_ManaForm _uc = new Locomotive.Repair.TrainRepair.Locomotive_Repair_OneTrain_Work_Main_ManaForm();
            //_uc._LoadID = _TrainSpiritSelected.iBMWID.ToString();
            //_uc.ShowDialog();
        }
        #endregion
        #region//--配件控件事件--//
        /// <summary>
        /// 先载入有检修配件的台位，图片随机显示一个批次的图片，文字提示显示系统分类信息
        /// </summary>
        private void _InitFittingsSpirit()
        {
            _CleanFittingsSpirit();
            string strSQL = "select f_id,f_name,(select stuff((select '#' + f_name from TS_S_T_P_TrainDepSys t where f_id in (select f_tdid from TS_FI_BU_Work_Main where f_tpmapid=TS_S_B_Station_WorkPlace.f_id and f_del=0) and f_del = TS_S_T_P_TrainDepSys.f_del for xml path('')) , 1 , 1 , '') as f_name from TS_S_T_P_TrainDepSys where f_id in (select f_tdid from TS_FI_BU_Work_Main where f_tpmapid=TS_S_B_Station_WorkPlace.f_id) group by f_del ) as f_tdlistname,(select f_points from TS_S_B_R_Digital_Map_Main_Element where f_del=0 and f_dmmid=" + cb_BackGround_List.SelectedValue.ToString() + " and f_swid=TS_S_B_Station_WorkPlace.f_id) as f_points,(select (select f_size from TS_S_B_Digital_Map_Element where f_id=TS_S_B_R_Digital_Map_Main_Element.f_dmeid) from TS_S_B_R_Digital_Map_Main_Element where f_del=0 and f_dmmid=" + cb_BackGround_List.SelectedValue.ToString() + " and f_swid=TS_S_B_Station_WorkPlace.f_id) as f_size,(select top 1 f_typeid from TS_FI_BU_Work_Main where f_tpmapid=TS_S_B_Station_WorkPlace.f_id and f_del=0 and f_state in (select f_id from TS_FI_B_Work_Main_State where f_del=0 and f_state<>9) order by f_id asc) as f_ttid,(select top 1 f_mwsid from TS_FI_BU_Work_Main_Steps where f_del=0 and f_bmwid=(select top 1 f_id from TS_FI_BU_Work_Main where f_tpmapid=TS_S_B_Station_WorkPlace.f_id and f_del=0 and f_state in (select f_id from TS_FI_B_Work_Main_State where f_del=0 and f_state<>9) order by f_id asc) and f_state in (select f_id from TS_FI_B_Work_Main_Steps_State where f_del=0 and f_state<>9) order by f_order asc) as f_mwsid from TS_S_B_Station_WorkPlace where f_del=0 and f_id in (select f_tpmapid from TS_FI_BU_Work_Main where f_del=0 and f_state in (select f_id from TS_FI_B_Work_Main_State where f_del=0 and f_state<>9))" + _sQueryMapResult;
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            SqlCommand comm = new SqlCommand(strSQL, conn);
            SqlDataReader read = comm.ExecuteReader();
            while (read.Read())
            {
                if (Grid_UC.FindName("Fittings_" + read["f_id"].ToString().Trim()) == null)
                {
                    LoadFittingsSpritControll(read["f_size"].ToString().Trim(), "Fittings_" + read["f_id"].ToString().Trim(), read["f_name"].ToString().Trim(), read["f_tdlistname"].ToString().Trim().Replace('#', '\r'), read["f_points"].ToString().Trim(), 1, (int)read["f_id"], 1, _GetFittingsImg((int)read["f_ttid"], (int)read["f_mwsid"]), (int)read["f_id"], (int)read["f_id"]);
                }
            }
            read.Close();
            read.Dispose();
            comm.Dispose();
            conn.Close();
            conn.Dispose();
        }
        private void LoadFittingsSpritControll(string sSize, string sName, string sTrainName, string sRPName, string sXY, int iTrainState, int iTrainID, int iWorkState, BitmapImage _Image, int iBMWID, int iWorkPlaceID)
        {
            double _x, _y;
            _FIWorkPlaceWidth = Convert.ToDouble(sSize.Substring(0, sSize.IndexOf(',')));
            _FIWorkPlaceHeight = Convert.ToDouble(sSize.Substring(sSize.IndexOf(',') + 1));
            RepairFittingsSpirit Spirit = new RepairFittingsSpirit();
            Spirit.ToolTip = FittingsSpiritToolTip(iWorkPlaceID.ToString());
            Spirit.Name = sName;
            Spirit.iBMWID = iBMWID;
            Spirit.iWorkPlaceID = iWorkPlaceID;
            Spirit.iTrainID = iTrainID;
            Spirit.iTrainState = iTrainState;
            Spirit.iWorkState = iWorkState;
            Spirit.MouseMove += new System.Windows.Input.MouseEventHandler(FittingsMouseMove);
            Spirit.MouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(FittingsMouseLeftButtonDown);
            Spirit.MouseLeftButtonUp += new System.Windows.Input.MouseButtonEventHandler(FittingsMouseLeftButtonUp);
            //Spirit.MouseDoubleClick += new System.Windows.Input.MouseButtonEventHandler(TrainMouseDoubleClick);
            Spirit._Name.Text = sRPName;
            Spirit._Name.Fill = new SolidColorBrush(Colors.YellowGreen);
            //Spirit._RPName.Text = sRPName;
            //Spirit._RPName.Fill = new SolidColorBrush(Colors.YellowGreen);
            Spirit._image.Source = _Image;
            _x = Convert.ToDouble(sXY.Substring(0, sXY.IndexOf(',')));
            _y = Convert.ToDouble(sXY.Substring(sXY.IndexOf(',') + 1));
            Spirit.CenterX = _x;
            Spirit.CenterY = _y;
            Spirit.Width = _FIWorkPlaceWidth / (_MapWidth / _Map.Width);
            Spirit.Height = _FIWorkPlaceHeight * 3.00 / (_MapHeight / _Map.Height);
            Spirit._grid.Width = _FIWorkPlaceWidth / (_MapWidth / _Map.Width);
            Spirit._grid.Height = _FIWorkPlaceHeight * 3.00 / (_MapHeight / _Map.Height);
            Spirit.iHeight = _Image.Height;
            Spirit.iWidth = _Image.Width;
            Spirit._image.Width = _FIWorkPlaceWidth / (_MapWidth / _Map.Width);
            Spirit._image.Height = _FIWorkPlaceHeight / (_MapHeight / _Map.Height);
            Spirit._Name.Margin = new System.Windows.Thickness(0, 0 - Spirit.Height * 2.00 / 3.00, 0, 0);
            //Spirit._RPName.Margin = new System.Windows.Thickness(0, Spirit.Height * 2.00 / 3.00 + Spirit._image.Height / 2.00, 0, 0);
            Spirit._image.Margin = new System.Windows.Thickness(0, Spirit.Height / 3.00, 0, 0);
            //Spirit._Name.Margin = new System.Windows.Thickness(0, 0 - Spirit.Height * 2.00 / 3.00, 0, 0);
            //Spirit._RPName.Margin = new System.Windows.Thickness(0, Spirit.Height * 2.00 / 3.00+Spirit._image.Height/2.00, 0, 0);
            //Spirit._image.Margin = new System.Windows.Thickness(0, Spirit.Height*2 / 3.00, 0, 0);
            if (Spirit._image.Height <= 12.00)
            {
                //Spirit._RPName.FontSize = 6.00;
                Spirit._Name.FontSize = 6.00;
            }
            if (Spirit._image.Height <= 24.00 && Spirit._image.Height > 12.00)
            {
                //Spirit._RPName.FontSize = 9.00;
                Spirit._Name.FontSize = 9.00;
            }
            if (Spirit._image.Height <= 36.00 && Spirit._image.Height > 24.00)
            {
                //Spirit._RPName.FontSize = 12.00;
                Spirit._Name.FontSize = 12.00;
            }
            if (Spirit._image.Height > 36.00)
            {
                //Spirit._RPName.FontSize = 16.00;
                Spirit._Name.FontSize = 16.00;
            }
            _img_Fittings.Add(Spirit.Name, Spirit);
            Grid_UC.RegisterName(Spirit.Name, Spirit);
            Grid_UC.Children.Add(Spirit);
            //Spirit.Background = new SolidColorBrush(Colors.White);
            Panel.SetZIndex(Spirit, 2);
            Canvas.SetLeft(Spirit, (_x) / (_MapWidth / _Map.Width) + _MapX);
            //Canvas.SetTop(Spirit, (_y - _WorkPlaceHeight) / (_MapHeight / _Map.Height) + _MapY);// -  
            Canvas.SetTop(Spirit, (_y + _FIWorkPlaceHeight) / (_MapHeight / _Map.Height) - Spirit._image.Height * 7.00 / 4.00 + _MapY);// -  
        }
        private void ReLoadFittingsSpritControll(string sSize, string sName, string sTrainName, string sRPName, string sXY, int iTrainState, int iTrainID, int iWorkState, BitmapImage _Image, int iBMWID, int iWorkPlaceID)
        {
            double _x, _y;
            _FIWorkPlaceWidth = Convert.ToDouble(sSize.Substring(0, sSize.IndexOf(',')));
            _FIWorkPlaceHeight = Convert.ToDouble(sSize.Substring(sSize.IndexOf(',') + 1));
            RepairFittingsSpirit Spirit = (RepairFittingsSpirit)Grid_UC.FindName(sName); ;
            Spirit.ToolTip = FittingsSpiritToolTip(iWorkPlaceID.ToString());
            Spirit.Name = sName;
            Spirit.iBMWID = iBMWID;
            Spirit.iWorkPlaceID = iWorkPlaceID;
            Spirit.iTrainID = iTrainID;
            Spirit.iTrainState = iTrainState;
            Spirit.iWorkState = iWorkState;
            //Spirit.MouseMove += new System.Windows.Input.MouseEventHandler(TrainMouseMove);
            //Spirit.MouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(TrainMouseLeftButtonDown);
            //Spirit.MouseLeftButtonUp += new System.Windows.Input.MouseButtonEventHandler(TrainMouseLeftButtonUp);
            //Spirit.MouseDoubleClick += new System.Windows.Input.MouseButtonEventHandler(TrainMouseDoubleClick);
            Spirit._Name.Text = sRPName;
            Spirit._Name.Fill = new SolidColorBrush(Colors.YellowGreen);
            //Spirit._RPName.Text = sRPName;
            //Spirit._RPName.Fill = new SolidColorBrush(Colors.YellowGreen);
            Spirit._image.Source = _Image;
            _x = Convert.ToDouble(sXY.Substring(0, sXY.IndexOf(',')));
            _y = Convert.ToDouble(sXY.Substring(sXY.IndexOf(',') + 1));
            Spirit.CenterX = _x;
            Spirit.CenterY = _y;
            Spirit.Width = _FIWorkPlaceWidth / (_MapWidth / _Map.Width);
            Spirit.Height = _FIWorkPlaceHeight * 3.00 / (_MapHeight / _Map.Height);
            Spirit._grid.Width = _FIWorkPlaceWidth / (_MapWidth / _Map.Width);
            Spirit._grid.Height = _FIWorkPlaceHeight * 3.00 / (_MapHeight / _Map.Height);
            Spirit.iHeight = _Image.Height;
            Spirit.iWidth = _Image.Width;
            Spirit._image.Width = _FIWorkPlaceWidth / (_MapWidth / _Map.Width);
            Spirit._image.Height = _FIWorkPlaceHeight / (_MapHeight / _Map.Height);
            Spirit._Name.Margin = new System.Windows.Thickness(0, 0 - Spirit.Height * 2.00 / 3.00, 0, 0);
            //Spirit._RPName.Margin = new System.Windows.Thickness(0, Spirit.Height * 2.00 / 3.00 + Spirit._image.Height / 2.00, 0, 0);
            Spirit._image.Margin = new System.Windows.Thickness(0, Spirit.Height / 3.00, 0, 0);
            //Spirit._Name.Margin = new System.Windows.Thickness(0, 0 - Spirit.Height * 2.00 / 3.00, 0, 0);
            //Spirit._RPName.Margin = new System.Windows.Thickness(0, Spirit.Height * 2.00 / 3.00+Spirit._image.Height/2.00, 0, 0);
            //Spirit._image.Margin = new System.Windows.Thickness(0, Spirit.Height*2 / 3.00, 0, 0);
            if (Spirit._image.Height <= 12.00)
            {
                //Spirit._RPName.FontSize = 6.00;
                Spirit._Name.FontSize = 6.00;
            }
            if (Spirit._image.Height <= 24.00 && Spirit._image.Height > 12.00)
            {
                //Spirit._RPName.FontSize = 9.00;
                Spirit._Name.FontSize = 9.00;
            }
            if (Spirit._image.Height <= 36.00 && Spirit._image.Height > 24.00)
            {
                //Spirit._RPName.FontSize = 12.00;
                Spirit._Name.FontSize = 12.00;
            }
            if (Spirit._image.Height > 36.00)
            {
                //Spirit._RPName.FontSize = 16.00;
                Spirit._Name.FontSize = 16.00;
            }
        }
        private void _CleanFittingsSpirit()
        {
            foreach (KeyValuePair<string, RepairFittingsSpirit> _Equpment_sort in _img_Fittings)
            {
                Grid_UC.UnregisterName(_Equpment_sort.Key);
                Grid_UC.Children.Remove(_Equpment_sort.Value);
            }
            _img_Fittings.Clear();
        }
        private void _RefreshFittings()
        {
            RepairFittingsSpirit Spirit = new RepairFittingsSpirit();
            foreach (KeyValuePair<string, RepairFittingsSpirit> _Train_sort in _img_Fittings)
            {
                Spirit = _Train_sort.Value;
                if (Spirit != null)
                {
                    Spirit.Width = _FIWorkPlaceWidth / (_MapWidth / _Map.Width);
                    Spirit.Height = _FIWorkPlaceHeight * 3 / (_MapHeight / _Map.Height);
                    Spirit.iHeight = _FIWorkPlaceHeight * 3 / (_MapHeight / _Map.Height);
                    Spirit.iWidth = _FIWorkPlaceWidth / (_MapWidth / _Map.Width);
                    Spirit._image.Width = _FIWorkPlaceWidth / (_MapWidth / _Map.Width);
                    Spirit._image.Height = _FIWorkPlaceHeight / (_MapHeight / _Map.Height);
                    Spirit._grid.Width = _FIWorkPlaceWidth / (_MapWidth / _Map.Width);
                    Spirit._grid.Height = _FIWorkPlaceHeight * 3 / (_MapHeight / _Map.Height);
                    Spirit._Name.Margin = new System.Windows.Thickness(0, 0 - Spirit.Height * 2 / 3, 0, 0);
                    //Spirit._RPName.Margin = new System.Windows.Thickness(0, Spirit.Height * 2 / 3 + Spirit._image.Height / 2, 0, 0);
                    Spirit._image.Margin = new System.Windows.Thickness(0, Spirit.Height / 3, 0, 0);
                    if (Spirit._image.Height <= 12.00)
                    {
                        //Spirit._RPName.FontSize = 6.00;
                        Spirit._Name.FontSize = 6.00;
                    }
                    if (Spirit._image.Height <= 24.00 && Spirit._image.Height > 12.00)
                    {
                        //Spirit._RPName.FontSize = 9.00;
                        Spirit._Name.FontSize = 9.00;
                    }
                    if (Spirit._image.Height <= 36.00 && Spirit._image.Height > 24.00)
                    {
                        //Spirit._RPName.FontSize = 12.00;
                        Spirit._Name.FontSize = 12.00;
                    }
                    if (Spirit._image.Height > 36.00)
                    {
                        //Spirit._RPName.FontSize = 16.00;
                        Spirit._Name.FontSize = 16.00;
                    }
                    Canvas.SetLeft(Spirit, (Spirit.CenterX) / (_MapWidth / _Map.Width) + Canvas.GetLeft(_Map));
                    Canvas.SetTop(Spirit, (Spirit.CenterY + _FIWorkPlaceHeight) / (_MapHeight / _Map.Height) - Spirit._image.Height * 7.00 / 4.00 + Canvas.GetTop(_Map));// - Spirit.Height*2/3
                }
                else
                {

                }
            }

        }
        private BitmapImage _GetFittingsImg(int _ttid, int _mwsid)
        {
            BitmapImage bitmapImage;
            bitmapImage = new BitmapImage();
            string _str = TS_Win_Class.GetSQlFieldValue("select top 1 f_brief from TS_S_B_Digital_Map_FittingsType_Steps where f_del=0 and f_ttid=" + _ttid.ToString() + " and f_mwsid=" + _mwsid.ToString(), "f_brief");
            try
            {
                if (_str.Length > 100)
                {
                    byte[] bt = Convert.FromBase64String(_str);
                    System.IO.MemoryStream stream = new System.IO.MemoryStream(bt);
                    stream.Seek(0, SeekOrigin.Begin);
                    bitmapImage.BeginInit();

                    bitmapImage.StreamSource = stream;

                    bitmapImage.EndInit();
                }
            }
            catch
            {
            }
            return bitmapImage;
        }
        private ToolTip FittingsSpiritToolTip(string _TPMAPID)
        {
            ToolTip ttp = new ToolTip();
            //ToolTipService.SetInitialShowDelay(ttp,600000);
            //ToolTipService.SetIsEnabled(ttp, true);
            //ttp.Opacity = 0.85;
            //ttp.IsOpen = false;
            //ttp.IsOpen = true;
            //ttp.StaysOpen = true;
            WrapPanel _spA = new WrapPanel();
            _spA.Orientation = Orientation.Horizontal;
            string strSQL = "select (select f_string from TS_S_T_S_ColorList where f_id=(select f_clid from TS_FI_B_Work_Main_State where f_id=TS_FI_BU_Work_Main.f_state)) as f_clcode,f_id,CONVERT(varchar(10), f_stime, 120) as f_stime,CONVERT(varchar(10), f_etime, 120) as f_etime,f_workcode as f_name,f_typeid as f_ttid,f_stname,f_time,f_tpmapid,(select f_name from TS_S_B_Station_WorkPlace where f_id=TS_FI_BU_Work_Main.f_tpmapid) as f_mapname,(select top 1 f_name from TS_FI_BU_Work_Main_Steps where f_del=0 and f_bmwid=TS_FI_BU_Work_Main.f_id and f_state in (select f_id from TS_FI_B_Work_Main_Steps_State where f_del=0 and f_state<>9) order by f_order asc) as f_mwsname,(select stuff((select '#' + f_name+'  编号:'+f_code from TS_FI_B_Loco_Fittings_Base_List t where f_id in (select f_fid from TS_FI_BU_Work_Main_Fittings_List where f_del=0 and f_bmwid=TS_FI_BU_Work_Main.f_id) and f_typeid = TS_FI_B_Loco_Fittings_Base_List.f_typeid for xml path('')) , 1 , 1 , '') as f_name from TS_FI_B_Loco_Fittings_Base_List where f_id in (select f_fid from TS_FI_BU_Work_Main_Fittings_List where f_del=0 and f_bmwid=TS_FI_BU_Work_Main.f_id) group by f_typeid) as f_listname  from TS_FI_BU_Work_Main where f_del=0 and f_state in (select f_id from TS_FI_B_Work_Main_State where f_del=0 and f_state<>9) and f_tpmapid=" + _TPMAPID;
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            SqlCommand comm = new SqlCommand(strSQL, conn);
            SqlDataReader read = comm.ExecuteReader();
            while (read.Read())
            {
                StackPanel _sp = new StackPanel();
                _sp.Orientation = Orientation.Horizontal;
                TextBlock _tb = new TextBlock();
                _tb.Background = new SolidColorBrush(Colors.AliceBlue);
                _tb = new TextBlock();
                switch (read["f_clcode"].ToString())
                {
                    case "Gray":
                        _tb.Background = new SolidColorBrush(Colors.Gray);
                        break;
                    case "Red":
                        _tb.Background = new SolidColorBrush(Colors.Red);
                        break;
                    case "Yellow":
                        _tb.Background = new SolidColorBrush(Colors.Yellow);
                        break;
                    case "Blue":
                        _tb.Background = new SolidColorBrush(Colors.Blue);
                        break;
                    case "Magenta":
                        _tb.Background = new SolidColorBrush(Colors.Magenta);
                        break;
                    case "Green":
                        _tb.Background = new SolidColorBrush(Colors.Green);
                        break;
                    case "White":
                        _tb.Background = new SolidColorBrush(Colors.White);
                        break;
                    default:
                        _tb.Background = new SolidColorBrush(Colors.White);
                        break;
                }
                _tb.Text = "批次:" + read["f_name"].ToString() + "\r";
                _tb.Text += "来源:" + read["f_stname"].ToString() + "\r";
                _tb.Text += "配件列表:" + "\r" + read["f_listname"].ToString().Replace('#', '\r') + "\r";
                _tb.Text += "作业位置:" + read["f_mapname"].ToString() + "\r";
                _tb.Text += "作业状态:" + read["f_mwsname"].ToString() + "\r";
                _tb.Text += "入库时间:" + read["f_stime"].ToString() + "\r";
                _tb.Text += "计划出库时间:" + read["f_etime"].ToString() + "\r";
                _sp.Children.Add(_tb);
                _spA.Children.Add(_sp);
            }
            read.Close();
            read.Dispose();
            comm.Dispose();
            conn.Close();
            conn.Dispose();
            ttp.Content = _spA;
            //ToolTipService.SetShowDuration(ttp, 60000);
            return (ttp);
        }
        private void FittingsMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (_IsInfoWindowOpen)
            {
            }
            else
            {
                if (_FittingsSpiritSelected != null && _IsTrainLeftKeyDown)
                {
                    Canvas.SetLeft(_FittingsSpiritSelected, e.GetPosition(Grid_UC).X - _TrainMouseX);
                    Canvas.SetTop(_FittingsSpiritSelected, (e.GetPosition(Grid_UC).Y) - _TrainMouseY);
                }
            }
        }
        private void FittingsMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            //tb_Text.Text = _IsInfoWindowOpen.ToString() + _IsTrainLeftKeyDown.ToString();
            if (_IsLeftKeyDown || _IsInfoWindowOpen)
            {
                _IsTrainLeftKeyDown = false;
                _FittingsSpiritSelected = null;
            }
            else
            {
                dispatcherTimer.Stop();
                _IsTrainLeftKeyDown = true;
                _FittingsSpiritSelected = (RepairFittingsSpirit)sender;
                _TrainMouseX = e.GetPosition(Grid_UC).X - Canvas.GetLeft(_FittingsSpiritSelected);
                _TrainMouseY = e.GetPosition(Grid_UC).Y - Canvas.GetTop(_FittingsSpiritSelected);
                //tb_Text.Text = sender.GetType().ToString().Substring(sender.GetType().ToString().LastIndexOf('.') + 1);
            }
        }
        private void FittingsMouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (_IsTrainLeftKeyDown)
            {
                Double _x = 0.00;
                Double _y = 0.00;
                string strSQL = "select * from TS_S_B_R_Digital_Map_Main_Element where f_del=0 and f_dmmid=" + cb_BackGround_List.SelectedValue.ToString() + " and f_swid in (select f_id from TS_S_B_Station_WorkPlace where f_del=0 and f_stack>f_stacknum and f_type in (1))";
                SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
                conn.Open();
                SqlCommand comm = new SqlCommand(strSQL, conn);
                SqlDataReader read = comm.ExecuteReader();
                while (read.Read())
                {
                    _x = Convert.ToDouble(read["f_points"].ToString().Substring(0, read["f_points"].ToString().IndexOf(',')));
                    _y = Convert.ToDouble(read["f_points"].ToString().Substring(read["f_points"].ToString().IndexOf(',') + 1));
                    if (e.GetPosition(_Map).X >= _x * (_Map.Width / _MapWidth) && e.GetPosition(_Map).X <= _x * (_Map.Width / _MapWidth) + _FittingsSpiritSelected.Width && e.GetPosition(_Map).Y <= _y * (_Map.Height / _MapHeight) + _FittingsSpiritSelected.Height && e.GetPosition(_Map).Y >= _y * (_Map.Height / _MapHeight))
                    {
                        ////tb_Text.Text = "找到工位";
                        //strSQL = "update TS_S_B_Station_WorkPlace set f_stacknum=f_stacknum-1 where f_id=" + _FittingsSpiritSelected.iWorkPlaceID.ToString();
                        //TS_Win_Class.MySqlCom(strSQL);
                        //strSQL = "update TS_S_B_Station_WorkPlace set f_stacknum=f_stacknum+1 where f_id=" + read["f_swid"].ToString();
                        //TS_Win_Class.MySqlCom(strSQL);
                        //strSQL = "update TS_TR_BU_Work_Main set f_tpmapid=" + read["f_swid"].ToString() + " where f_id=" + _FittingsSpiritSelected.iBMWID.ToString();
                        //TS_Win_Class.MySqlCom(strSQL);
                        _FittingsSpiritSelected.CenterX = _x;
                        _FittingsSpiritSelected.CenterY = _y;
                        //_FittingsSpiritSelected.iWorkPlaceID = (int)read["f_swid"];
                        break;
                    }
                }
                read.Close();
                read.Dispose();
                comm.Dispose();
                conn.Close();
                conn.Dispose();
                _RefreshFittings();
            }
            dispatcherTimer.Start();
            _IsTrainLeftKeyDown = false;
            _FittingsSpiritSelected = null;
            //tb_Text.Text = "左键弹起";
        }
        #endregion
        #region//--设备控件事件--//
        // 初始化设备控件
        private void _InitEqupmentSpirit()
        {
            _CleanEqupmentSpirit();
            string strSQL = "select f_id,f_eqname as f_name,f_etid as f_ttid,f_rpname,f_time,f_tpmapid,(select f_points from TS_S_B_R_Digital_Map_Main_Element where f_del=0 and f_dmmid=" + cb_BackGround_List.SelectedValue.ToString() + " and f_swid=TS_EQ_BU_Work_Main.f_tpmapid) as f_points,(select (select f_size from TS_S_B_Digital_Map_Element where f_id=TS_S_B_R_Digital_Map_Main_Element.f_dmeid) from TS_S_B_R_Digital_Map_Main_Element where f_del=0 and f_dmmid=" + cb_BackGround_List.SelectedValue.ToString() + " and f_swid=TS_EQ_BU_Work_Main.f_tpmapid) as f_size,(select top 1 f_mwsid from TS_EQ_BU_Work_Main_Steps where f_del=0 and f_bmwid=TS_EQ_BU_Work_Main.f_id and f_state in (select f_id from TS_EQ_B_Work_Main_Steps_State where f_del=0 and f_state<>9) order by f_order asc) as f_mwsid  from TS_EQ_BU_Work_Main where f_del=0 and f_state in (select f_id from TS_EQ_B_Work_Main_State where f_del=0 and f_state<>9) and f_tpmapid in (select f_id from TS_S_B_Station_WorkPlace where f_del=0 " + _sQueryMapResult + ")";
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            SqlCommand comm = new SqlCommand(strSQL, conn);
            SqlDataReader read = comm.ExecuteReader();
            while (read.Read())
            {
                if (Grid_UC.FindName("Equpment_" + read["f_id"].ToString().Trim()) == null)
                {
                    LoadEqupmentSpritControll(read["f_size"].ToString().Trim(), "Equpment_" + read["f_id"].ToString().Trim(), read["f_name"].ToString().Trim(), read["f_rpname"].ToString().Trim(), read["f_points"].ToString().Trim(), 1, (int)read["f_id"], 1, _GetEqupmentImg((int)read["f_ttid"], (int)read["f_mwsid"]), (int)read["f_id"], (int)read["f_tpmapid"]);
                }
            }
            read.Close();
            read.Dispose();
            comm.Dispose();
            conn.Close();
            conn.Dispose();
        }
        private void LoadEqupmentSpritControll(string sSize, string sName, string sTrainName, string sRPName, string sXY, int iTrainState, int iTrainID, int iWorkState, BitmapImage _Image, int iBMWID, int iWorkPlaceID)
        {
            double _x, _y;
            _EQWorkPlaceWidth = Convert.ToDouble(sSize.Substring(0, sSize.IndexOf(',')));
            _EQWorkPlaceHeight = Convert.ToDouble(sSize.Substring(sSize.IndexOf(',') + 1));
            RepairEqupmentSpirit Spirit = new RepairEqupmentSpirit();
            Spirit.ToolTip = EqupmentSpiritToolTip(iBMWID.ToString());
            Spirit.Name = sName;
            Spirit.iBMWID = iBMWID;
            Spirit.iWorkPlaceID = iWorkPlaceID;
            Spirit.iTrainID = iTrainID;
            Spirit.iTrainState = iTrainState;
            Spirit.iWorkState = iWorkState;
            Spirit.MouseMove += new System.Windows.Input.MouseEventHandler(EqupmentMouseMove);
            Spirit.MouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(EqupmentMouseLeftButtonDown);
            Spirit.MouseLeftButtonUp += new System.Windows.Input.MouseButtonEventHandler(EqupmentMouseLeftButtonUp);
            Spirit.MouseRightButtonDown += new System.Windows.Input.MouseButtonEventHandler(EqupmentMouseRightButtonDown);
            Spirit._Name.Text = sTrainName;
            Spirit._Name.Fill = new SolidColorBrush(Colors.YellowGreen);
            //Spirit._RPName.Text = sRPName;
            //Spirit._RPName.Fill = new SolidColorBrush(Colors.YellowGreen);
            Spirit._image.Source = _Image;
            _x = Convert.ToDouble(sXY.Substring(0, sXY.IndexOf(',')));
            _y = Convert.ToDouble(sXY.Substring(sXY.IndexOf(',') + 1));
            Spirit.CenterX = _x;
            Spirit.CenterY = _y;
            Spirit.Width = _EQWorkPlaceWidth / (_MapWidth / _Map.Width);
            Spirit.Height = _EQWorkPlaceHeight * 3.00 / (_MapHeight / _Map.Height);
            Spirit._grid.Width = _EQWorkPlaceWidth / (_MapWidth / _Map.Width);
            Spirit._grid.Height = _EQWorkPlaceHeight * 3.00 / (_MapHeight / _Map.Height);
            Spirit.iHeight = _Image.Height;
            Spirit.iWidth = _Image.Width;
            Spirit._image.Width = _EQWorkPlaceWidth / (_MapWidth / _Map.Width);
            Spirit._image.Height = _EQWorkPlaceHeight / (_MapHeight / _Map.Height);
            Spirit._Name.Margin = new System.Windows.Thickness(0, 0 - Spirit.Height * 2.00 / 3.00, 0, 0);
            //Spirit._RPName.Margin = new System.Windows.Thickness(0, Spirit.Height * 2.00 / 3.00 + Spirit._image.Height / 2.00, 0, 0);
            Spirit._image.Margin = new System.Windows.Thickness(0, Spirit.Height / 3.00, 0, 0);
            //Spirit._Name.Margin = new System.Windows.Thickness(0, 0 - Spirit.Height * 2.00 / 3.00, 0, 0);
            //Spirit._RPName.Margin = new System.Windows.Thickness(0, Spirit.Height * 2.00 / 3.00+Spirit._image.Height/2.00, 0, 0);
            //Spirit._image.Margin = new System.Windows.Thickness(0, Spirit.Height*2 / 3.00, 0, 0);
            if (Spirit._image.Height <= 12.00)
            {
                //Spirit._RPName.FontSize = 6.00;
                Spirit._Name.FontSize = 6.00;
            }
            if (Spirit._image.Height <= 24.00 && Spirit._image.Height > 12.00)
            {
                //Spirit._RPName.FontSize = 9.00;
                Spirit._Name.FontSize = 9.00;
            }
            if (Spirit._image.Height <= 36.00 && Spirit._image.Height > 24.00)
            {
                //Spirit._RPName.FontSize = 12.00;
                Spirit._Name.FontSize = 12.00;
            }
            if (Spirit._image.Height > 36.00)
            {
                //Spirit._RPName.FontSize = 16.00;
                Spirit._Name.FontSize = 16.00;
            }
            _img_Equpment.Add(Spirit.Name, Spirit);
            Grid_UC.RegisterName(Spirit.Name, Spirit);
            Grid_UC.Children.Add(Spirit);
            //Spirit.Background = new SolidColorBrush(Colors.White);
            Panel.SetZIndex(Spirit, 2);
            Canvas.SetLeft(Spirit, (_x) / (_MapWidth / _Map.Width) + _MapX);
            //Canvas.SetTop(Spirit, (_y - _WorkPlaceHeight) / (_MapHeight / _Map.Height) + _MapY);// -  
            Canvas.SetTop(Spirit, (_y + _EQWorkPlaceHeight) / (_MapHeight / _Map.Height) - Spirit._image.Height * 7.00 / 4.00 + _MapY);// -  
        }
        private void ReLoadEqupmentSpritControll(string sSize, string sName, string sTrainName, string sRPName, string sXY, int iTrainState, int iTrainID, int iWorkState, BitmapImage _Image, int iBMWID, int iWorkPlaceID)
        {
            double _x, _y;
            _EQWorkPlaceWidth = Convert.ToDouble(sSize.Substring(0, sSize.IndexOf(',')));
            _EQWorkPlaceHeight = Convert.ToDouble(sSize.Substring(sSize.IndexOf(',') + 1));
            RepairEqupmentSpirit Spirit = (RepairEqupmentSpirit)Grid_UC.FindName(sName);
            Spirit.ToolTip = EqupmentSpiritToolTip(iBMWID.ToString());
            Spirit.Name = sName;
            Spirit.iBMWID = iBMWID;
            Spirit.iWorkPlaceID = iWorkPlaceID;
            Spirit.iTrainID = iTrainID;
            Spirit.iTrainState = iTrainState;
            Spirit.iWorkState = iWorkState;
            Spirit._Name.Text = sTrainName;
            Spirit._Name.Fill = new SolidColorBrush(Colors.YellowGreen);
            //Spirit._RPName.Text = sRPName;
            //Spirit._RPName.Fill = new SolidColorBrush(Colors.YellowGreen);
            Spirit._image.Source = _Image;
            _x = Convert.ToDouble(sXY.Substring(0, sXY.IndexOf(',')));
            _y = Convert.ToDouble(sXY.Substring(sXY.IndexOf(',') + 1));
            Spirit.CenterX = _x;
            Spirit.CenterY = _y;
            Spirit.Width = _EQWorkPlaceWidth / (_MapWidth / _Map.Width);
            Spirit.Height = _EQWorkPlaceHeight * 3.00 / (_MapHeight / _Map.Height);
            Spirit._grid.Width = _EQWorkPlaceWidth / (_MapWidth / _Map.Width);
            Spirit._grid.Height = _EQWorkPlaceHeight * 3.00 / (_MapHeight / _Map.Height);
            Spirit.iHeight = _Image.Height;
            Spirit.iWidth = _Image.Width;
            Spirit._image.Width = _EQWorkPlaceWidth / (_MapWidth / _Map.Width);
            Spirit._image.Height = _EQWorkPlaceHeight / (_MapHeight / _Map.Height);
            Spirit._Name.Margin = new System.Windows.Thickness(0, 0 - Spirit.Height * 2.00 / 3.00, 0, 0);
            //Spirit._RPName.Margin = new System.Windows.Thickness(0, Spirit.Height * 2.00 / 3.00 + Spirit._image.Height / 2.00, 0, 0);
            Spirit._image.Margin = new System.Windows.Thickness(0, Spirit.Height / 3.00, 0, 0);
            if (Spirit._image.Height <= 12.00)
            {
                //Spirit._RPName.FontSize = 6.00;
                Spirit._Name.FontSize = 6.00;
            }
            if (Spirit._image.Height <= 24.00 && Spirit._image.Height > 12.00)
            {
                //Spirit._RPName.FontSize = 9.00;
                Spirit._Name.FontSize = 9.00;
            }
            if (Spirit._image.Height <= 36.00 && Spirit._image.Height > 24.00)
            {
                //Spirit._RPName.FontSize = 12.00;
                Spirit._Name.FontSize = 12.00;
            }
            if (Spirit._image.Height > 36.00)
            {
                //Spirit._RPName.FontSize = 16.00;
                Spirit._Name.FontSize = 16.00;
            }
        }
        private ToolTip EqupmentSpiritToolTip(string _BMWID)
        {
            ToolTip ttp = new ToolTip();
            StackPanel _sp = new StackPanel();
            string strSQL = "select (select f_string from TS_S_T_S_ColorList where f_id=(select f_clid from TS_EQ_B_Work_Main_State where f_id=TS_EQ_BU_Work_Main.f_state)) as f_clcode,f_id,CONVERT(varchar(10), f_stime, 120) as f_stime,CONVERT(varchar(10), f_etime, 120) as f_etime,f_eqname as f_name,f_etid as f_ttid,f_rpname as f_rpnum,(select f_name from TS_EQ_B_Loco_Equipment_Repair_CycleTime where f_id=TS_EQ_BU_Work_Main.f_rpid) as f_rpname,f_time,f_tpmapid,(select f_name from TS_S_B_Station_WorkPlace where f_id=TS_EQ_BU_Work_Main.f_tpmapid) as f_mapname,(select top 1 f_name from TS_EQ_BU_Work_Main_Steps where f_del=0 and f_bmwid=TS_EQ_BU_Work_Main.f_id and f_state in (select f_id from TS_EQ_B_Work_Main_Steps_State where f_del=0 and f_state<>9) order by f_order asc) as f_mwsname  from TS_EQ_BU_Work_Main where f_del=0 and f_id=" + _BMWID;
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            SqlCommand comm = new SqlCommand(strSQL, conn);
            SqlDataReader read = comm.ExecuteReader();
            while (read.Read())
            {
                TextBlock _tb = new TextBlock();
                switch (read["f_clcode"].ToString())
                {
                    case "Gray":
                        _tb.Background = new SolidColorBrush(Colors.Gray);
                        break;
                    case "Red":
                        _tb.Background = new SolidColorBrush(Colors.Red);
                        break;
                    case "Yellow":
                        _tb.Background = new SolidColorBrush(Colors.Yellow);
                        break;
                    case "Blue":
                        _tb.Background = new SolidColorBrush(Colors.Blue);
                        break;
                    case "Magenta":
                        _tb.Background = new SolidColorBrush(Colors.Magenta);
                        break;
                    case "Green":
                        _tb.Background = new SolidColorBrush(Colors.Green);
                        break;
                    case "White":
                        _tb.Background = new SolidColorBrush(Colors.White);
                        break;
                    default:
                        _tb.Background = new SolidColorBrush(Colors.White);
                        break;
                }
                _tb.Text = "设备名:" + read["f_name"].ToString() + "\r";
                _tb.Text += "修程:" + read["f_rpname"].ToString() + "\r";
                _tb.Text += "修次:" + read["f_rpnum"].ToString() + "\r";
                _tb.Text += "作业位置:" + read["f_mapname"].ToString() + "\r";
                _tb.Text += "作业状态:" + read["f_mwsname"].ToString() + "\r";
                _tb.Text += "入库时间:" + read["f_stime"].ToString() + "\r";
                _tb.Text += "计划出库时间:" + read["f_etime"].ToString() + "\r";
                _sp.Children.Add(_tb);
            }
            read.Close();
            read.Dispose();
            comm.Dispose();
            conn.Close();
            conn.Dispose();

            ttp.Content = _sp;
            //ToolTipService.SetShowDuration(ttp, 60000);
            return (ttp);
        }
        private void _RefreshEqupment()
        {
            RepairEqupmentSpirit Spirit = new RepairEqupmentSpirit();
            foreach (KeyValuePair<string, RepairEqupmentSpirit> _Train_sort in _img_Equpment)
            {
                Spirit = _Train_sort.Value;
                if (Spirit != null)
                {
                    Spirit.Width = _EQWorkPlaceWidth / (_MapWidth / _Map.Width);
                    Spirit.Height = _EQWorkPlaceHeight * 3 / (_MapHeight / _Map.Height);
                    Spirit.iHeight = _EQWorkPlaceHeight * 3 / (_MapHeight / _Map.Height);
                    Spirit.iWidth = _EQWorkPlaceWidth / (_MapWidth / _Map.Width);
                    Spirit._image.Width = _EQWorkPlaceWidth / (_MapWidth / _Map.Width);
                    Spirit._image.Height = _EQWorkPlaceHeight / (_MapHeight / _Map.Height);
                    Spirit._grid.Width = _EQWorkPlaceWidth / (_MapWidth / _Map.Width);
                    Spirit._grid.Height = _EQWorkPlaceHeight * 3 / (_MapHeight / _Map.Height);
                    Spirit._Name.Margin = new System.Windows.Thickness(0, 0 - Spirit.Height * 2 / 3, 0, 0);
                    //Spirit._RPName.Margin = new System.Windows.Thickness(0, Spirit.Height * 2 / 3 + Spirit._image.Height / 2, 0, 0);
                    Spirit._image.Margin = new System.Windows.Thickness(0, Spirit.Height / 3, 0, 0);
                    if (Spirit._image.Height <= 12.00)
                    {
                        //Spirit._RPName.FontSize = 6.00;
                        Spirit._Name.FontSize = 6.00;
                    }
                    if (Spirit._image.Height <= 24.00 && Spirit._image.Height > 12.00)
                    {
                        //Spirit._RPName.FontSize = 9.00;
                        Spirit._Name.FontSize = 9.00;
                    }
                    if (Spirit._image.Height <= 36.00 && Spirit._image.Height > 24.00)
                    {
                        //Spirit._RPName.FontSize = 12.00;
                        Spirit._Name.FontSize = 12.00;
                    }
                    if (Spirit._image.Height > 36.00)
                    {
                        //Spirit._RPName.FontSize = 16.00;
                        Spirit._Name.FontSize = 16.00;
                    }
                    Canvas.SetLeft(Spirit, (Spirit.CenterX) / (_MapWidth / _Map.Width) + Canvas.GetLeft(_Map));
                    Canvas.SetTop(Spirit, (Spirit.CenterY + _EQWorkPlaceHeight) / (_MapHeight / _Map.Height) - Spirit._image.Height * 7.00 / 4.00 + Canvas.GetTop(_Map));// - Spirit.Height*2/3
                }
                else
                {

                }
            }

        }
        private void _CleanEqupmentSpirit()
        {
            foreach (KeyValuePair<string, RepairEqupmentSpirit> _Train_sort in _img_Equpment)
            {
                Grid_UC.UnregisterName(_Train_sort.Key);
                Grid_UC.Children.Remove(_Train_sort.Value);
            }
            _img_Equpment.Clear();
        }
        private BitmapImage _GetEqupmentImg(int _ttid, int _mwsid)
        {
            BitmapImage bitmapImage;
            bitmapImage = new BitmapImage();
            string _str = TS_Win_Class.GetSQlFieldValue("select top 1 f_brief from TS_S_B_Digital_Map_EquipmentType_Steps where f_del=0 and f_ttid=" + _ttid.ToString() + " and f_mwsid=" + _mwsid.ToString(), "f_brief");
            try
            {
                if (_str.Length > 100)
                {
                    byte[] bt = Convert.FromBase64String(_str);
                    System.IO.MemoryStream stream = new System.IO.MemoryStream(bt);
                    stream.Seek(0, SeekOrigin.Begin);
                    bitmapImage.BeginInit();

                    bitmapImage.StreamSource = stream;

                    bitmapImage.EndInit();
                }
            }
            catch
            {
            }
            return bitmapImage;
        }
        private void EqupmentMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (_IsInfoWindowOpen)
            {
            }
            else
            {
                if (_EqupmentSpiritSelected != null && _IsTrainLeftKeyDown)
                {
                    Canvas.SetLeft(_EqupmentSpiritSelected, e.GetPosition(Grid_UC).X - _TrainMouseX);
                    Canvas.SetTop(_EqupmentSpiritSelected, (e.GetPosition(Grid_UC).Y) - _TrainMouseY);
                }
            }
        }
        private void EqupmentMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            //tb_Text.Text = _IsInfoWindowOpen.ToString() + _IsTrainLeftKeyDown.ToString();
            if (_IsLeftKeyDown || _IsInfoWindowOpen)
            {
                _IsTrainLeftKeyDown = false;
                _EqupmentSpiritSelected = null;
            }
            else
            {
                dispatcherTimer.Stop();
                _IsTrainLeftKeyDown = true;
                _EqupmentSpiritSelected = (RepairEqupmentSpirit)sender;
                _TrainMouseX = e.GetPosition(Grid_UC).X - Canvas.GetLeft(_EqupmentSpiritSelected);
                _TrainMouseY = e.GetPosition(Grid_UC).Y - Canvas.GetTop(_EqupmentSpiritSelected);
                //tb_Text.Text = sender.GetType().ToString().Substring(sender.GetType().ToString().LastIndexOf('.') + 1);
            }
        }
        private void EqupmentMouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (_IsTrainLeftKeyDown)
            {
                Double _x = 0.00;
                Double _y = 0.00;
                string strSQL = "select * from TS_S_B_R_Digital_Map_Main_Element where f_del=0 and f_dmmid=" + cb_BackGround_List.SelectedValue.ToString() + " and f_swid in (select f_id from TS_S_B_Station_WorkPlace where f_del=0 and f_stack>f_stacknum and f_type in (2))";
                SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
                conn.Open();
                SqlCommand comm = new SqlCommand(strSQL, conn);
                SqlDataReader read = comm.ExecuteReader();
                while (read.Read())
                {
                    _x = Convert.ToDouble(read["f_points"].ToString().Substring(0, read["f_points"].ToString().IndexOf(',')));
                    _y = Convert.ToDouble(read["f_points"].ToString().Substring(read["f_points"].ToString().IndexOf(',') + 1));
                    if (e.GetPosition(_Map).X >= _x * (_Map.Width / _MapWidth) && e.GetPosition(_Map).X <= _x * (_Map.Width / _MapWidth) + _TrainSpiritSelected.Width && e.GetPosition(_Map).Y <= _y * (_Map.Height / _MapHeight) + _TrainSpiritSelected.Height && e.GetPosition(_Map).Y >= _y * (_Map.Height / _MapHeight))
                    {
                        //tb_Text.Text = "找到工位";
                        strSQL = "update TS_S_B_Station_WorkPlace set f_stacknum=f_stacknum-1 where f_id=" + _EqupmentSpiritSelected.iWorkPlaceID.ToString();
                        TS_Win_Class.MySqlCom(strSQL);
                        strSQL = "update TS_S_B_Station_WorkPlace set f_stacknum=f_stacknum+1 where f_id=" + read["f_swid"].ToString();
                        TS_Win_Class.MySqlCom(strSQL);
                        strSQL = "update TS_EQ_BU_Work_Main set f_tpmapid=" + read["f_swid"].ToString() + " where f_id=" + _EqupmentSpiritSelected.iBMWID.ToString();
                        TS_Win_Class.MySqlCom(strSQL);
                        _EqupmentSpiritSelected.CenterX = _x;
                        _EqupmentSpiritSelected.CenterY = _y;
                        _EqupmentSpiritSelected.iWorkPlaceID = (int)read["f_swid"];
                        break;
                    }
                }
                read.Close();
                read.Dispose();
                comm.Dispose();
                conn.Close();
                conn.Dispose();
                _RefreshEqupment();
            }
            dispatcherTimer.Start();
            _IsTrainLeftKeyDown = false;
            _EqupmentSpiritSelected = null;
            //tb_Text.Text = "左键弹起";
        }
        private void EqupmentMouseRightButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            //_IsInfoWindowOpen = true;
            //_IsTrainLeftKeyDown = false;
            //_EqupmentSpiritSelected = (RepairEqupmentSpirit)sender;
            //Pub.MFD.Locomotive.Equpment.Pub_MDF_Locomotive_Equipment_Main_Work_Show_Form _uc = new Pub.MFD.Locomotive.Equpment.Pub_MDF_Locomotive_Equipment_Main_Work_Show_Form();
            //_uc._BMWID = _EqupmentSpiritSelected.iBMWID.ToString();
            //if (_uc.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            //{
            //    _IsInfoWindowOpen = false;
            //    _IsTrainLeftKeyDown = false;
            //    _EqupmentSpiritSelected = null;
            //}
            //else
            //{
            //    _IsInfoWindowOpen = false;
            //    _IsTrainLeftKeyDown = false;
            //    _EqupmentSpiritSelected = null;
            //}
            //_TrainMouseX = 0.00;
            //_TrainMouseY = 0.00;
        }
        #endregion
        #region//--实例图片--//
        private void _SetSampleImg()
        {
            int _YMove = 10;
            int _Width = 60;
            int _Height = 30;
            string strSQL = "select count(*) as f_count,f_cid from (select isnull((select top 1 (select f_cid from TS_S_T_S_RP_Name_Dictionary where f_id=TS_TR_BU_Work_Main_Day.f_ndid) from TS_TR_BU_Work_Main_Day where f_del=0 and f_bmwid=TS_TR_BU_Work_Main.f_id and f_state in (select f_id from TS_S_T_S_RP_DataState where f_del=0 and f_state<>9) order by f_order asc),0) as f_cid  from TS_TR_BU_Work_Main where f_del=0 and f_state in (select f_id from TS_S_T_S_RP_DataState where f_del=0 and f_state not in (-1,9)) ) as a group by a.f_cid";
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            SqlCommand comm = new SqlCommand(strSQL, conn);
            SqlDataReader read = comm.ExecuteReader();
            while (read.Read())
            {
                    switch ((int)read["f_cid"]+1)
                    {
                        case 1:
                            tb_hd3c_s0.Text = read["f_count"].ToString() + "台";
                            break;
                        case 2:
                            tb_hd3c_s1.Text = read["f_count"].ToString() + "台";
                            break;
                        case 3:
                            tb_hd3c_s2.Text = read["f_count"].ToString() + "台";
                            break;
                        case 4:
                            tb_hd3c_s3.Text = read["f_count"].ToString() + "台";
                            break;
                        case 5:
                            tb_hd3c_s4.Text = read["f_count"].ToString() + "台";
                            break;
                        case 6:
                            tb_hd3c_s5.Text = read["f_count"].ToString() + "台";
                            break;
                        case 7:
                            tb_hd3c_s6.Text = read["f_count"].ToString() + "台";
                            break;
                        case 8:
                            tb_hd3c_s7.Text = read["f_count"].ToString() + "台";
                            break;
                        default:
                            break;
                    }
            }
            read.Close();
            read.Dispose();
            comm.Dispose();
            conn.Close();
            conn.Dispose();
            for (int i = 0; i < 8; i++)
            {
                switch (i + 1)
                {
                    case 1:
                        Canvas.SetTop(tb_hd3c_s0, Grid_UC.ActualHeight - _Height * 3);
                        Canvas.SetLeft(tb_hd3c_s0, _Width * i + _YMove);
                        Canvas.SetZIndex(tb_hd3c_s0, 999);
                        //img_hd3c_0.Source = _GetTrainImg((int)read["f_ttid"], (int)read["f_id"]);
                        Canvas.SetTop(img_hd3c_0, Grid_UC.ActualHeight - _Height * 2);
                        Canvas.SetLeft(img_hd3c_0, _Width * i + _YMove);
                        Canvas.SetZIndex(img_hd3c_0, 999);
                        //tb_hd3c_0.Text = read["f_name"].ToString();
                        Canvas.SetTop(tb_hd3c_0, Grid_UC.ActualHeight - _Height * 1);
                        Canvas.SetLeft(tb_hd3c_0, _Width * i + _YMove);
                        Canvas.SetZIndex(tb_hd3c_0, 999);
                        break;
                    case 2:
                        Canvas.SetTop(tb_hd3c_s1, Grid_UC.ActualHeight - _Height * 3);
                        Canvas.SetLeft(tb_hd3c_s1, _Width * i + _YMove);
                        Canvas.SetZIndex(tb_hd3c_s1, 999);
                        //img_hd3c_1.Source = _GetTrainImg((int)read["f_ttid"], (int)read["f_id"]);
                        Canvas.SetTop(img_hd3c_1, Grid_UC.ActualHeight - _Height * 2);
                        Canvas.SetLeft(img_hd3c_1, _Width * i + _YMove);
                        Canvas.SetZIndex(img_hd3c_1, 999);
                        //tb_hd3c_1.Text = read["f_name"].ToString();
                        Canvas.SetTop(tb_hd3c_1, Grid_UC.ActualHeight - _Height * 1);
                        Canvas.SetLeft(tb_hd3c_1, _Width * i + _YMove);
                        Canvas.SetZIndex(tb_hd3c_1, 999);
                        break;
                    case 3:
                        Canvas.SetTop(tb_hd3c_s2, Grid_UC.ActualHeight - _Height * 3);
                        Canvas.SetLeft(tb_hd3c_s2, _Width * i + _YMove);
                        Canvas.SetZIndex(tb_hd3c_s2, 999);
                        //img_hd3c_2.Source = _GetTrainImg((int)read["f_ttid"], (int)read["f_id"]);
                        Canvas.SetTop(img_hd3c_2, Grid_UC.ActualHeight - _Height * 2);
                        Canvas.SetLeft(img_hd3c_2, _Width * i + _YMove);
                        Canvas.SetZIndex(img_hd3c_2, 999);
                        //tb_hd3c_2.Text = read["f_name"].ToString();
                        Canvas.SetTop(tb_hd3c_2, Grid_UC.ActualHeight - _Height * 1);
                        Canvas.SetLeft(tb_hd3c_2, _Width * i + _YMove);
                        Canvas.SetZIndex(tb_hd3c_2, 999);
                        break;
                    case 4:
                        Canvas.SetTop(tb_hd3c_s3, Grid_UC.ActualHeight - _Height * 3);
                        Canvas.SetLeft(tb_hd3c_s3, _Width * i + _YMove);
                        Canvas.SetZIndex(tb_hd3c_s3, 999);
                        //img_hd3c_3.Source = _GetTrainImg((int)read["f_ttid"], (int)read["f_id"]);
                        Canvas.SetTop(img_hd3c_3, Grid_UC.ActualHeight - _Height * 2);
                        Canvas.SetLeft(img_hd3c_3, _Width * i + _YMove);
                        Canvas.SetZIndex(img_hd3c_3, 999);
                        //tb_hd3c_3.Text = read["f_name"].ToString();
                        Canvas.SetTop(tb_hd3c_3, Grid_UC.ActualHeight - _Height * 1);
                        Canvas.SetLeft(tb_hd3c_3, _Width * i + _YMove);
                        Canvas.SetZIndex(tb_hd3c_3, 999);
                        break;
                    case 5:
                        Canvas.SetTop(tb_hd3c_s4, Grid_UC.ActualHeight - _Height * 3);
                        Canvas.SetLeft(tb_hd3c_s4, _Width * i + _YMove);
                        Canvas.SetZIndex(tb_hd3c_s4, 999);
                        //img_hd3c_4.Source = _GetTrainImg((int)read["f_ttid"], (int)read["f_id"]);
                        Canvas.SetTop(img_hd3c_4, Grid_UC.ActualHeight - _Height * 2);
                        Canvas.SetLeft(img_hd3c_4, _Width * i + _YMove);
                        Canvas.SetZIndex(img_hd3c_4, 999);
                        //tb_hd3c_4.Text = read["f_name"].ToString();
                        Canvas.SetTop(tb_hd3c_4, Grid_UC.ActualHeight - _Height * 1);
                        Canvas.SetLeft(tb_hd3c_4, _Width * i + _YMove);
                        Canvas.SetZIndex(tb_hd3c_4, 999);
                        break;
                    case 6:
                        Canvas.SetTop(tb_hd3c_s5, Grid_UC.ActualHeight - _Height * 3);
                        Canvas.SetLeft(tb_hd3c_s5, _Width * i + _YMove);
                        Canvas.SetZIndex(tb_hd3c_s5, 999);
                        //img_hd3c_5.Source = _GetTrainImg((int)read["f_ttid"], (int)read["f_id"]);
                        Canvas.SetTop(img_hd3c_5, Grid_UC.ActualHeight - _Height * 2);
                        Canvas.SetLeft(img_hd3c_5, _Width * i + _YMove);
                        Canvas.SetZIndex(img_hd3c_5, 999);
                        //tb_hd3c_5.Text = read["f_name"].ToString();
                        Canvas.SetTop(tb_hd3c_5, Grid_UC.ActualHeight - _Height * 1);
                        Canvas.SetLeft(tb_hd3c_5, _Width * i + _YMove);
                        Canvas.SetZIndex(tb_hd3c_5, 999);
                        break;
                    case 7:
                        Canvas.SetTop(tb_hd3c_s6, Grid_UC.ActualHeight - _Height * 3);
                        Canvas.SetLeft(tb_hd3c_s6, _Width * i + _YMove);
                        Canvas.SetZIndex(tb_hd3c_s6, 999);
                        //img_hd3c_6.Source = _GetTrainImg((int)read["f_ttid"], (int)read["f_id"]);
                        Canvas.SetTop(img_hd3c_6, Grid_UC.ActualHeight - _Height * 2);
                        Canvas.SetLeft(img_hd3c_6, _Width * i + _YMove);
                        Canvas.SetZIndex(img_hd3c_6, 999);
                        //tb_hd3c_6.Text = read["f_name"].ToString();
                        Canvas.SetTop(tb_hd3c_6, Grid_UC.ActualHeight - _Height * 1);
                        Canvas.SetLeft(tb_hd3c_6, _Width * i + _YMove);
                        Canvas.SetZIndex(tb_hd3c_6, 999);
                        break;
                    case 8:
                        Canvas.SetTop(tb_hd3c_s7, Grid_UC.ActualHeight - _Height * 3);
                        Canvas.SetLeft(tb_hd3c_s7, _Width * i + _YMove);
                        Canvas.SetZIndex(tb_hd3c_s7, 999);
                        //img_hd3c_6.Source = _GetTrainImg((int)read["f_ttid"], (int)read["f_id"]);
                        Canvas.SetTop(img_hd3c_7, Grid_UC.ActualHeight - _Height * 2);
                        Canvas.SetLeft(img_hd3c_7, _Width * i + _YMove);
                        Canvas.SetZIndex(img_hd3c_7, 999);
                        //tb_hd3c_6.Text = read["f_name"].ToString();
                        Canvas.SetTop(tb_hd3c_7, Grid_UC.ActualHeight - _Height * 1);
                        Canvas.SetLeft(tb_hd3c_7, _Width * i + _YMove);
                        Canvas.SetZIndex(tb_hd3c_7, 999);
                        break;
                    default:
                        break;
                }
            }
        }
        #endregion
        private void UserControl_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            try
            {
                for (int i = 0; i < _img_DevForm.Count - 5; i++)
                {
                    ((DevExpress.XtraEditors.XtraForm)_img_DevForm["_video" + i.ToString()]).Close();
                }
                ((DevExpress.XtraEditors.XtraForm)_img_DevForm["MFD"]).Close();
                ((DevExpress.XtraEditors.XtraForm)_img_DevForm["DEP"]).Close();
                ((DevExpress.XtraEditors.XtraForm)_img_DevForm["REP"]).Close();
                ((DevExpress.XtraEditors.XtraForm)_img_DevForm["IMG"]).Close();
                ((DevExpress.XtraEditors.XtraForm)_img_DevForm["VOI"]).Close();
                _img_DevForm.Clear();
            }
            catch
            {
            }
        }

    }
}
